import java.util.NoSuchElementException; public class ListStack implements MyStack { /******************************************************* The private inner class "Node" *******************************************************/ // make Node class name of generic type private class Node { // Node instance variable is generic private T item; private Node next; // parameters in constructor generic public Node(T item, Node next) { this.item = item; this.next = next; } public String toString(){ return "" + this.item; } } // End of private inner class "Node" private Node first; // Constructs an empty list public ListStack() { first = null; } // Returns true if the listStack is empty public boolean isEmpty() { return first==null; } // Returns true if the listStack is full public boolean isFull() { return false; // A linked list is never full... } // push(item) --> insert at the front of the list public void push(T item) { Node newNode = new Node(item, first); first = newNode; } // pop() --> remove the first item and return it public T pop() { if(isEmpty()) { throw new NoSuchElementException(); } T toReturn = first.item; first = first.next; return toReturn; } // peek() public T peek() { if(isEmpty()) { throw new NoSuchElementException(); } return first.item; } // Returns a string representation public String toString() { String output = ""; if(first == null) return "[NULL]"; Node tmp = first; while(tmp != null) { output += tmp + " -> "; tmp = tmp.next; } output += "[NULL]"; return output; } } // End of class