Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stack
- {
- int[] stack;
- int top = -1;
- int max;
- public Stack()
- {
- int size = 5;
- stack = new int[size];
- max = size;
- }
- public Stack(int size)
- {
- stack = new int[size];
- max = size;
- }
- public void push(int n)
- {
- if (top > max - 1)
- {
- Console.WriteLine("Stack overflow");
- }
- else
- {
- stack[++top] = n;
- }
- }
- public int pop()
- {
- if (top == -1)
- {
- Console.WriteLine("Stack is empty");
- return -1;
- }
- else
- {
- Console.WriteLine("{0} has been popped from the stack", stack[top]);
- return stack[top--];
- }
- }
- public int topOfStack()
- {
- if (top == -1)
- {
- Console.WriteLine("Stack is empty");
- return -1;
- }
- else
- {
- Console.WriteLine("{0} is the top of the stack", stack[top]);
- return stack[top];
- }
- }
- public void showStack()
- {
- if(top == -1)
- {
- Console.WriteLine("Stack is empty");
- }
- else
- {
- Console.WriteLine("Stack: ");
- for(int i = 0; i < top; i++)
- {
- Console.WriteLine(stack[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement