varungurnaney

C#: IEnumerator with Stack Class

Jul 11th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Generic_Stack_Implementation
  8.     {
  9.     class Mystack<T> : IEnumerable
  10.         {
  11.         T[] list = null;
  12.         int top;
  13.  
  14.         public void push(T element)
  15.             {
  16.             top++;
  17.             list[top] = element;
  18.  
  19.             }
  20.  
  21.         public Mystack(int size)
  22.             {
  23.             list = new T[size];
  24.             int top = -1;
  25.             }
  26.  
  27.         public T pop()
  28.             {
  29.  
  30.             return (list[top--]);
  31.             }
  32.  
  33.         public IEnumerator GetEnumerator()
  34.             {
  35.             return list.GetEnumerator();
  36.             }
  37.         }
  38.  
  39.     class Program
  40.         {
  41.         static void Main(string[] args)
  42.             {
  43.             Mystack<int> mys = new Mystack<int>(5);
  44.             mys.push(200);
  45.             mys.push(300);
  46.             mys.push(500);
  47.             Console.WriteLine(mys.pop()); //pop 500
  48.             Console.WriteLine(mys.pop());//pop 300
  49.             IEnumerator ie = mys.GetEnumerator();
  50.             while (ie.MoveNext())
  51.                 {
  52.                 Console.WriteLine(ie.Current);
  53.                 }
  54.             Console.ReadKey();
  55.  
  56.             }
  57.         }
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment