/********************************************************** * 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; // make class name of generic type public class GenericLinkedList implements SimpleList { /******************************************************* 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 and returns the first element (item) in the list. // returns generic type public T removeFirst() { if(isEmpty()) { throw new NoSuchElementException(); } T toReturn = first.item; first = first.next; return toReturn; } // Inserts a new node to the end of this list // take in generic item; Nodes are generic public void addLast(T item) { if(isEmpty()) { first = new Node(item,null); //equivalent: addFirst(item) } else { Node current = first; while(current.next!=null) { current = current.next; } current.next = new Node(item,null); } } // Delete the node at the end of this list public T removeLast() { if(isEmpty()) // Edge case 1: empty list { // empty list can't remove anything throw new NoSuchElementException(); } if( first.next == null ) // Edge case 2: list has 1 elem { Node ret = first; first = null; return ret.item; } // General case // Find the last node while remembering the previous node Node current = first; Node previous = first; while( current.next != null ) { previous = current; current = current.next; } // Unlink the last node (from its predecessor) previous.next = null; // Return the last node return current.item; } // Returns the first element (item) in the list // returns generic type public T getFirst() { if(isEmpty()) { throw new NoSuchElementException(); } return first.item; } // Returns the last element (item) in the list // returns generic type; Nodes are generic public T getLast() { if(isEmpty()) { throw new NoSuchElementException(); } Node current = first; while(current.next!=null) { current = current.next; } return current.item; } // Returns the item at the specified position in the list. // Note: Assume that pos is the index of the node, and that // node indexes start at zero! // returns generic type; Nodes are generic public T get(int pos) { // Coding strategy: Start by solving the general case, then add checks // for edge cases (e.g. What if "pos" is out-of-bound?) if(isEmpty()) { // empty list throw new NoSuchElementException(); } int i = 0; Node current = first; while(i 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; } } // Reverse the linked list! // Example LL before reversing: // A => B => C => D => NULL // Example LL after reversing: // D => C => B => A => NULL // Nodes are generic public void reverse() { if(isEmpty()) { throw new NoSuchElementException(); } Node current1 = first; // iterate through old list in old order Node current2 = first; // iterate through new list to make new links while(current1.next != null) { Node newNode = new Node(current1.next.item, current2); current2 = newNode; current1 = current1.next; } first.next = null; first = current2; } // TESTING public static void main(String[] args) { System.out.println("GenericLinkedList"); GenericLinkedList stringList = new GenericLinkedList(); stringList.addFirst("A"); stringList.addFirst("B"); stringList.addLast("C"); System.out.println(stringList); // B -> A -> C System.out.println("getFirst: " + stringList.getFirst()); // B System.out.println("getLast: " + stringList.getLast()); // C System.out.println("get(1): " + stringList.get(1)); // A stringList.remove("A"); System.out.println("List after remove(\"A\")"); System.out.println(stringList); // B -> C System.out.println(); // =========================================================== System.out.println("GenericLinkedList"); GenericLinkedList intList = new GenericLinkedList(); intList.addFirst(1); // ooh fancy word for this? intList.addFirst(2); intList.addLast(3); System.out.println(intList); // 2 -> 1 -> 3 System.out.println("getFirst: " + intList.getFirst()); // 2 System.out.println("getLast: " + intList.getLast()); // 3 System.out.println("get(1): " + intList.get(1)); // 1 intList.remove(1); System.out.println("List after remove(1)"); System.out.println(intList); // 2 -> 3 } } // End of class