Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- /**
- @Author: Sajmon
- */
- namespace IGenerics
- {
- interface IData {
- bool IsEmpty();
- bool IsFull();
- void Clear();
- }
- public class GenericStack<T> : IData {
- private T[] stack;
- private int size;
- private int index;
- public GenericStack(int size)
- {
- this.size = size;
- this.index = 0;
- this.stack = new T[size];
- }
- public void Push(T Number)
- {
- if (!IsFull())
- {
- stack[index] = Number;
- index++;
- }
- else {
- throw new ApplicationException("Stack overflow");
- }
- }
- public T Pop() {
- if (!IsEmpty())
- {
- T temp = stack[index];
- stack[index] = default(T);
- index--;
- return temp;
- }
- else
- {
- throw new ApplicationException("Stack underflow");
- }
- }
- public T Top() {
- if (!IsEmpty())
- {
- return stack[size - 1];
- }
- else
- {
- throw new ApplicationException("Stack underflow");
- }
- }
- public bool IsEmpty()
- {
- return index == 0;
- }
- public bool IsFull()
- {
- return index == size - 1;
- }
- public void Clear()
- {
- stack = null;
- index = 0;
- stack = new T[size];
- }
- }
- public class GenericQueue<T> : IData {
- private T[] queue;
- private int size;
- private int index;
- public GenericQueue(int size) {
- this.size = size;
- this.queue = new T[size];
- this.index = 0;
- }
- public void Add(T item) {
- queue[index] = item;
- index++;
- }
- public T Get() {
- if (!IsEmpty())
- {
- T temp = queue[0];
- int x = 0;
- while (x < size - 1)
- {
- queue[x] = queue[x + 1]; // 1,2,3,4,5
- x++;
- }
- queue[size - 1] = default(T);
- index--;
- return temp;
- }
- else
- {
- throw new ApplicationException("Stack underflow");
- }
- }
- public bool IsEmpty()
- {
- return index == 0;
- }
- public bool IsFull()
- {
- return index == size - 1;
- }
- public void Clear()
- {
- queue = null;
- index = 0;
- queue = new T[size];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment