class Node { Item data; Node next; public Node(Item data, Node next) { this.data = data; this.next = next; } } public class LinkedList { private Node front; public LinkedList() { front = null; } /** @return true iff the list is empty */ public boolean isEmpty() { return front == null; } /** * Inserts a new node in front of the list. * @param aNode a Node to be inserted into the list */ public void insert(Node aNode) { aNode.next = front; front = aNode; } /** * Deletes the front element of the list. * @return the value of the deleted node */ public Item delete() { if (isEmpty()) throw new RuntimeException("Trying to delete an empty list."); Item val = front.data; front = front.next; return val; } /** * Inspects the front element of the list. * @return the value of the data in the node at front of the list */ public Item peek() { if (isEmpty()) throw new RuntimeException("Trying to peek at an empty list."); return front.data; } /* @return the string representation of this list. */ public String toString() { StringBuilder sb = new StringBuilder(); for (Node node = front; node != null; node = node.next) { sb.append(" " + node.data); } return sb.toString(); } }