Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Zasobnik : IZasobnik, IEnumerable {
- private int size;
- private int index;
- private int[] stackArr;
- public IEnumerator GetEnumerator() {
- return new EnumStack(this);
- }
- #region Properties
- public int[] StackArr {
- get {
- return this.stackArr;
- }
- set {
- this.stackArr = value;
- }
- }
- public int Index {
- get {
- return this.index;
- }
- set {
- this.index = value;
- }
- }
- public int Size {
- get {
- return this.size;
- }
- set {
- this.index = value;
- }
- }
- #endregion
- public Zasobnik(int n) {
- this.index = 0;
- this.size = n;
- stackArr = new int[size];
- }
- public void Push(int Number)
- {
- if (!IsFull())
- {
- stackArr[index] = Number;
- Console.WriteLine("Item {0} was added to stack", Number);
- index++;
- }
- else
- throw new ApplicationException("Stack overflow");
- }
- public int Pop()
- {
- if (!IsEmpty())
- {
- int temp = stackArr[index - 1]; // vrchol zasobnika ulozim do premennej temp
- stackArr[index - 1] = 0; // zmazem vrchol zasobnika
- index--; // znizim index zasobnika
- //Console.WriteLine("Item {0} was removed from stack", temp);
- return temp; //vratim povodny vrchol zasobniku
- }
- else
- throw new ApplicationException("Stack underflow");
- }
- public int Top()
- {
- if (!IsEmpty())
- {
- return stackArr[index - 1];
- }
- else
- throw new ApplicationException("Stack underflow");
- }
- public bool IsEmpty()
- {
- return index == 0;
- }
- public bool IsFull()
- {
- return index == size - 1;
- }
- public void Clear()
- {
- stackArr = null;
- stackArr = new int[size];
- index = 0;
- }
- class EnumStack : IEnumerator {
- private Zasobnik stack;
- public EnumStack(Zasobnik z) {
- this.stack = z;
- }
- public object Current
- {
- get {
- return this.stack.StackArr[this.stack.Index];
- }
- }
- public bool MoveNext()
- {
- this.stack.Index--;
- return this.stack.Index > 0;
- }
- public void Reset()
- {
- this.stack.Index = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment