Implement Stack Operation In C#

  1. using System;  
  2. class Program {  
  3.     static void Main(string[] args) {  
  4.         int top = -1;  
  5.         int[] s = new int[10];  
  6.         Console.WriteLine("Enter The Size of The Stack");  
  7.         int MAX = Convert.ToInt16(Console.ReadLine());  
  8.         while (true) {  
  9.             Console.WriteLine("1.Push");  
  10.             Console.WriteLine("2.Pop");  
  11.             Console.WriteLine("3.Display");  
  12.             Console.WriteLine("4.Exit");  
  13.             Console.WriteLine("Enter your choice :");  
  14.             int ch = Convert.ToInt16(Console.ReadLine());  
  15.             switch (ch) {  
  16.                 case 1:  
  17.                     if (top > MAX - 1) Console.WriteLine("... Stack Overflow ...");  
  18.                     else {  
  19.                         Console.WriteLine("Enter the item :");  
  20.                         int n = int.Parse(Console.ReadLine());  
  21.                         s[++top] = n;  
  22.                     }  
  23.                     break;  
  24.                 case 2:  
  25.                     if (top == -1) Console.WriteLine(" ... Stack Underflow ...");  
  26.                     else {  
  27.                         Console.WriteLine("Popped item :" + s[top--]);  
  28.                     }  
  29.                     break;  
  30.                 case 3:  
  31.                     if (top == -1) Console.WriteLine("... Stack underflow ...");  
  32.                     else {  
  33.                         Console.WriteLine("Elements in the stack");  
  34.                         for (int i = top; i >= 0; i--) Console.WriteLine(s[i]);  
  35.                     }  
  36.                     break;  
  37.                 case 4:  
  38.                     return;  
  39.                 default:  
  40.                     Console.WriteLine("Wrong Choice");  
  41.                     break;  
  42.             }  
  43.         }  
  44.     }  
  45. }