//import java.util.Stack; public class stackApplication { public static void main(String[] args) { xStack s = new xStack(); if (s.isEmpty()) System.out.println("stack is empty"); s.push(new Integer(5)); s.push(new Integer(-2)); s.push(new Integer(1)); s.push(new Integer(99)); if (s.isEmpty()) System.out.println("stack is empty"); while (!s.isEmpty()) { System.out.println("top of stack value is : "+s.peek()); s.pop(); /* discard the return value from pop() */ } System.out.println("Stack empty, exiting ..."); } } interface StackInterface { /* returns true if stack is empty, false otherwise */ public boolean isEmpty() ; /* return true if stack is full, false otherwise */ public boolean isFull(); /* adds object to stack if stack is not full (let the user beware!) */ public void push(Object x); /* removes and returns object at the top of the stack, fails (ungracefully) if the stack is empty */ public Object pop(); /* returns object at the top of the stack, fails (ungracefully) if the stack is empty */ public Object peek(); } class xStack implements StackInterface { private Object[] s; private int n; public xStack() { s = new Object[100]; n = 0; } /* returns true if stack is empty, true otherwise */ public boolean isEmpty() {return (n == 0);} /* return true if stack is full, false otherwise */ public boolean isFull() { return (n == s.length);} /* adds object to stack if stack is not full (let the user beware!) */ public void push(Object x) { if (! isFull()) { s[n++] = x; } return;} /* removes and returns object at the top of the stack, fails (ungracefully) if the stack is empty */ public Object pop() { if (! isEmpty() ) { Object temp = s[n-1]; n--; return temp; } else return null; } /* returns object at the top of the stack, fails (ungracefully) if the stack is empty */ public Object peek() { if (! isEmpty() ) { return s[n-1]; } else return null; } }