/* Program 6.12 The Linked Stack Class */ /*-----------------------------------------*/ class StackNode { Object item; StackNode link; } /*-----------------------------------------*/ class Stack { private StackNode topNode; /*------*/ // note: Java's default no-arg constructor, new Stack(), // works fine for the linked stack representation // so there is no need to define one here /*------*/ public boolean empty() { return (topNode == null); } /*------*/ public void push(Object X) { StackNode newNode = new StackNode(); newNode.item = X; newNode.link = topNode; topNode = newNode; } /*------*/ public Object pop() { if (topNode == null) { return null; } else { StackNode tempNode = topNode; topNode = topNode.link; return tempNode.item; } } /*------*/ public Object peek() { if (topNode == null) { return null; } else { return topNode.item; } } /*------*/ } // end class Stack