Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyStack {
- int[] stack;
- int top;
- public MyStack() {
- stack = new int[5];
- top = -1;
- }
- public void push(int x) {
- if(top < stack.length-1){
- top++;
- stack[top] = x;
- }
- else{
- System.out.println("The stack is full");
- }
- }
- public int pop() {
- if(top >= 0){
- int elemento = stack[top];
- top--;
- return elemento;
- }
- else{
- System.out.println("The stack is empty");
- return -1;
- }
- }
- public int top() {
- if (top >= 0) {
- return stack[top];
- }
- else {
- System.out.println("The stack is empty");
- return -1;
- }
- }
- public boolean empty() {
- if(top < 0){
- return true;
- }
- else{
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement