public class NodeStack { private Node tos; //reference to the top of stack node private int size; //number of nodes in the stack public NodeStack(){tos = null; size = 0;} public int size(){return size;} public boolean isEmpty(){return tos == null;} public void push(E element){ tos = new Node(element,tos); size++; } // // NOTE: no overflow exception! // public E top() throws StackException { if (isEmpty()) throw new StackException("Stack empty"); return tos.getElement(); } public E pop() throws StackException { if (isEmpty()) throw new StackException("Stack underflow"); E element = tos.getElement(); tos = tos.getNext(); size--; return element; } private String toString(Node node){ if (node == null) return ""; return "," + node.getElement() + toString(node.getNext()); } public String toString(){ String s = ""; if (size > 0) s = s + tos.getElement() + toString(tos.getNext()); return "[" + s + "]"; } }