/********************************************************** * 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.NoSuchElementException; public class GenericLinkedList { /******************************************************* 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 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; } /* ****************************************************************/ // Assignment part 1: write the addAt(item, pos) method // // addAt(item, pos) inserts item at position pos in the list // // Examples: // addAt(item, 0) will insert item as the new first node // addAt(item, 1) will insert item after the first node // // Throw NoSuchElementException if pos is pass the last node /* ****************************************************************/ public void addAt(T item, int pos) { } /* ****************************************************************/ // Assignment part 2: Write the removeAt(pos) method // // removeAt(pos) Removes the node at position "pos" in the list // The item in the removed node is returned // // Examples: // removeAt(0) will remove the first node in list // removeAt(1) will remove the 2nd node in list // // Throw NoSuchElementException if pos is pass the last node /* ****************************************************************/ public T removeAt(int pos) { return null; // added so the program will compile... } } // End of class