hamzajaved

Push and Pop Method

Nov 6th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. class Program
  2.     {
  3.         struct Stack<G>
  4.         {
  5.  
  6.             public G[] arr;
  7.             public int top;
  8.             public void Push(G val)
  9.             {
  10.                 if (top < arr.Length - 1)
  11.                 {
  12.                     top++;
  13.                     arr[top] = val;
  14.                 }
  15.             }
  16.             public void Pop()
  17.             {
  18.                 if (top == -1)
  19.                 {
  20.                     Console.WriteLine("Stack is empty");
  21.                 }
  22.                 else
  23.                 {
  24.                     Console.WriteLine(arr[top]);
  25.                     top--;
  26.                 }
  27.             }
  28.         }  
  29.         static void Main(string[] args)
  30.         {
  31.             Stack<int> s;
  32.             s.top = -1;
  33.             s.arr = new int[5];
  34.             s.Push(1);
  35.             s.Push(5);
  36.             s.Push(7);
  37.             s.Pop();
  38.         }
  39.     }
Add Comment
Please, Sign In to add comment