//---------------------------------------------------------------------- // LinkedStack.java by Dale/Joyce/Weems Chapter 3 // // Implements UnboundedStackInterface using a linked list // to hold the stack elements. // ajg version: format, if-then-else //----------------------------------------------------------------------- package ch03.stacks; import support.LLNode; public class LinkedStack implements UnboundedStackInterface { protected LLNode top; // reference to the top of this stack public LinkedStack() { top = null; } public void push(T element) { LLNode newNode = new LLNode(element); newNode.setLink(top); top = newNode; } public void pop() { // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. if (isEmpty()) throw new StackUnderflowException("Helpful msg."); top = top.getLink(); } public T top() { // Throws StackUnderflowException if this stack is empty, // otherwise returns top element from this stack. if (isEmpty()) throw new StackUnderflowException("Helpful msg."); return top.getInfo(); } public boolean isEmpty() { return top == null; } } // Local Variables: // compile-command: "cd ../..; javac ch03/stacks/LinkedStack.java" // End: