/** * An IntStack is an object that holds a stack of ints. */ public class IntStack { private Node top = null; // The top Node in the stack /** * Test whether this stack has more elements. * @return true if this stack is not empty */ public boolean hasMore() { return (top != null); } /** * Pop the top int from this stack and return it. * This should be called only if the stack is * not empty. If called when the stack is empty, * we print an error message and exit the program. * @return the popped int */ public int pop() { Node n = top; if (n == null) { System.out.println("Popping an empty stack!"); System.exit(-1); } top = n.getLink(); return n.getData(); } /** * Push an int on top of this stack. * @param data the String to add */ public void push(int data) { top = new Node(data,top); } }