package stacks; public class NodeStack implements Stack{ private Node top; //reference to the head node private int size; //number of elements in the stack public NodeStack(){//constructs 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(E elem){ Node v=new Node(elem,top); //create and link-in a new node top = v; size++; } public E top() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException("Stack is empty."); return top.getElement(); } public E pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException("Stack is empty."); E temp = top.getElement(); top=top.getNext(); //link-out the former top node size--; return temp; } //AM added the following methods, to be more like ArrayStack implementation public String toString(){ String s; s="["; if (size()>0){Node temp=top; while (temp!=null){ s=s+temp.getElement()+", "; temp=temp.getNext(); } } s=s+"]"; return s; } // Prints status information about a recent operation and the stack public void status(String op, Object element){ System.out.print("------> " + op); //print this operation System.out.println(", returns " + element); //what was returned System.out.print("result: size = " + size() + ", isEmpty= " +isEmpty()); System.out.println(", stack: " + this); //contents of the stack } }