/********************************************************** * A Generic Linked List class with a private static inner Node class. *********************************************************/ // We want to "throw" Java exceptions in our code, // so we must import them first: import java.util.Iterator; import java.util.NoSuchElementException; // make class name of generic type public class GenericLinkedList implements SimpleList, Iterable { /******************************************************* 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" /********************************************************/ // SimpleLinkedList instance variable "first" is declared of // type "Node", i.e. the inner class defined above // first is generic node private Node first; // Constructs an empty list public GenericLinkedList() { first = null; } // Returns true if the list is empty public boolean isEmpty() { return first==null; } // 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; } // Inserts a new node at the beginning of this list // take in generic item; Nodes are generic public void addFirst(T item) { Node newNode = new Node(item, first); first = newNode; } // Removes the first occurrence of the specified element in this list. // key is generic type; Nodes are generic public void remove(T key) { if(isEmpty()) { // empty list can't remove anything throw new NoSuchElementException(); } if(first.item.equals(key)) // Edge case: first node contains key { first = first.next; // We must update the first variable! } else { // The general case updates the next field in the previous node // Find the previous node of the node that contains key Node current = first; Node previous = first; while( current!=null && !current.item.equals(key) ) { previous = current; current = current.next; } if(current==null) { // key not found throw new NoSuchElementException(); } previous.next = current.next; // Unlink the current node from list } } // These methods omited for brevity // They must be in the class to implement the SimpleList interface public T removeFirst() { return null; } public void addLast(T item) { } public T removeLast() { return null; } public T getFirst() { return null; } public T getLast() { return null; } public T get(int pos) { return null; } /*******************************************************/ // Since our class implements the interface Iterable, // it must implement the abstract method iterator() /*******************************************************/ public Iterator iterator() { return new MyLinkedListIterator(first); // See definition of MyLinkedListIterator class below } /******************************************************* * Private inner (nested) class MyLinkedListIterator * that implements the Java interface "Iterator" ********************************************************/ private class MyLinkedListIterator implements Iterator { private Node current; // Remember the current node (unprocessed) // Constructor public MyLinkedListIterator(Node f) { current = f; // the current node is the frst node } // Test if there is an unprcessed node public boolean hasNext() { return current != null; // we aren't at the last node } // next() returns the content of the current node and // MOVES to the next (unprocessed) node public T next() { if (!hasNext()) { throw new NoSuchElementException(); } T res = current.item; // get item at current position current = current.next; // move to the next node return res; } } // End of private inner class "MyLinkedListIterator" /********************************************************/ } // End of class