/**
 * Implements a linked version of the Stack interface.  This class is taken from
 * the book "Data Structurues and Algorithms in Java", Gooddrich and Tamassia 
 * @author Gooddrich and Tamassia 
 * @see Stack
 */

public class LinkedStack implements Stack {
       private Node top;             // reference to the head node
       private int size;             // number of elements in the stack   
       public LinkedStack() {        // initializes an empty stack
         top = null;
         size = 0;
       }
       public int size() {
         return size;
       }
       public boolean isEmpty() {
         if (top == null)
           return true;
         return false;
       }
       public void push(Object elem) {
         Node v = new Node();        // create a new node
         v.setElement(elem);
         v.setNext(top);             // link-in the new node
         top = v;
         size++;
       }
       public Object top() throws StackEmptyException {
         if (isEmpty())
           throw new StackEmptyException("Stack is empty.");
         return top.getElement();
       }
       public Object pop() throws StackEmptyException {
         if (isEmpty())
           throw new StackEmptyException("Stack is empty.");
         Object temp = top.getElement();
         top = top.getNext();        // link-out the former top node
         size--;
         return temp;
       }
}
