Sajmon

Stack with IEnumerable

Mar 19th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. public class Zasobnik : IZasobnik, IEnumerable {
  2.  
  3.             private int size;
  4.             private int index;
  5.             private int[] stackArr;
  6.  
  7.             public IEnumerator GetEnumerator() {
  8.                
  9.                 return new EnumStack(this);
  10.             }
  11.  
  12.             #region Properties
  13.             public int[] StackArr {
  14.            
  15.                 get {
  16.                     return this.stackArr;
  17.                 }
  18.                 set {
  19.                     this.stackArr = value;
  20.                 }
  21.             }
  22.  
  23.             public int Index {
  24.                 get {
  25.                     return this.index;
  26.                 }
  27.  
  28.                 set {
  29.                     this.index = value;
  30.                 }
  31.             }
  32.  
  33.             public int Size {
  34.                 get {
  35.                     return this.size;
  36.                 }
  37.                 set {
  38.                     this.index = value;
  39.                 }
  40.             }
  41.             #endregion
  42.  
  43.             public Zasobnik(int n) {
  44.                 this.index = 0;
  45.                 this.size = n;
  46.                 stackArr = new int[size];
  47.             }
  48.  
  49.             public void Push(int Number)
  50.             {
  51.                 if (!IsFull())
  52.                 {
  53.                     stackArr[index] = Number;
  54.                     Console.WriteLine("Item {0} was added to stack", Number);
  55.                     index++;
  56.                 }
  57.                 else
  58.                     throw new ApplicationException("Stack overflow");
  59.             }
  60.  
  61.             public int Pop()
  62.             {
  63.                 if (!IsEmpty())
  64.                 {
  65.                     int temp = stackArr[index - 1]; // vrchol zasobnika ulozim do premennej temp
  66.                     stackArr[index - 1] = 0; // zmazem vrchol zasobnika
  67.                     index--; // znizim index zasobnika
  68.                     //Console.WriteLine("Item {0} was removed from stack", temp);
  69.                     return temp; //vratim povodny vrchol zasobniku
  70.                 }
  71.                 else
  72.                     throw new ApplicationException("Stack underflow");
  73.             }
  74.  
  75.             public int Top()
  76.             {
  77.                 if (!IsEmpty())
  78.                 {
  79.                     return stackArr[index - 1];
  80.                 }
  81.                 else
  82.                     throw new ApplicationException("Stack underflow");
  83.             }
  84.  
  85.             public bool IsEmpty()
  86.             {
  87.                 return index == 0;
  88.             }
  89.  
  90.             public bool IsFull()
  91.             {
  92.                 return index == size - 1;
  93.             }
  94.  
  95.             public void Clear()
  96.             {
  97.                 stackArr = null;
  98.                 stackArr = new int[size];
  99.                 index = 0;
  100.             }
  101.  
  102.             class EnumStack : IEnumerator {
  103.  
  104.                 private Zasobnik stack;
  105.  
  106.                 public EnumStack(Zasobnik z) {
  107.                     this.stack = z;
  108.                 }
  109.  
  110.  
  111.                 public object Current
  112.                 {
  113.                     get {
  114.                         return this.stack.StackArr[this.stack.Index];
  115.                     }
  116.                 }
  117.  
  118.                 public bool MoveNext()
  119.                 {
  120.                     this.stack.Index--;
  121.                     return this.stack.Index > 0;
  122.                 }
  123.  
  124.                 public void Reset()
  125.                 {
  126.                     this.stack.Index = 0;
  127.                 }
  128.             }
  129.         }
Advertisement
Add Comment
Please, Sign In to add comment