Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class stack
- {
- private int[] array;
- private int pointer;
- public stack(int size)
- {
- this.array = new int[size];
- this.pointer = -1;
- }
- public void push(int n)
- {
- if (pointer == array.Length-1)
- {
- Console.WriteLine("Stack is full");
- }
- else
- {
- array[pointer+1] = n;
- pointer += 1;
- }
- }
- public void peek()
- {
- for (int i = 0;i<array.Length;i++)
- {
- Console.WriteLine(array[i]);
- }
- }
- public int pop()
- {
- if (pointer == -1)
- {
- Console.WriteLine("Stack is empty");
- return -1;
- }
- else
- {
- int temp = array[pointer];
- array[pointer] = 0;
- pointer -= 1;
- return temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement