import Stack; import Node; /** * An implementation of stacks using nodes. * * @author Samuel A. Rebelsky * @version 1.0 of April 1999 */ public class NodeBasedStack implements Stack { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The top of the stack. Set to null for empty stacks. */ protected Node top; /** The number of elements in the stack. */ protected int size; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new empty stack. */ public NodeBasedStack() { top = null; size = 0; } // NodeBasedStack() // +-----------+----------------------------------------------- // | Accessors | // +-----------+ /** * Is the stack full? Node-based stacks are never full. */ public boolean isFull() { return false; } // isFull() /** * Is the stack empty? */ public boolean isEmpty() { return top == null; } // isEmpty() /** * What's at the top of the stack? */ public Object peek() { return top.getContents(); } // peek() // +-----------+----------------------------------------------- // | Modifiers | // +-----------+ /** * Add an element to the stack. */ public void add(Object elt) { top = new Node(elt,top); } // add(Object) /** * Remove and return the top element from the stack. */ public Object get() { // Get the element at the top. Object tmp = top.getContents(); // Move on to the next element. top = top.getNext(); // Return the element. return tmp; } // get() } // class NodeBasedStack