Advertisement
logancberrypie

Simple Stack Class

Jun 3rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1.     class stack
  2.     {
  3.         private int[] array;
  4.         private int pointer;
  5.  
  6.         public stack(int size)
  7.         {
  8.             this.array = new int[size];
  9.             this.pointer = -1;
  10.         }
  11.         public void push(int n)
  12.         {
  13.             if (pointer == array.Length-1)
  14.             {
  15.                 Console.WriteLine("Stack is full");
  16.             }
  17.             else
  18.             {
  19.                 array[pointer+1] = n;
  20.                 pointer += 1;
  21.             }
  22.         }
  23.         public void peek()
  24.         {
  25.             for (int i = 0;i<array.Length;i++)
  26.             {
  27.                 Console.WriteLine(array[i]);
  28.             }
  29.         }
  30.         public int pop()
  31.         {
  32.             if (pointer == -1)
  33.             {
  34.                 Console.WriteLine("Stack is empty");
  35.                 return -1;
  36.             }
  37.             else
  38.             {
  39.                 int temp = array[pointer];
  40.                 array[pointer] = 0;
  41.                 pointer -= 1;
  42.                 return temp;
  43.             }
  44.         }
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement