import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; /** * Some experiments to better understand linked lists */ public class TestLinkedList { public static void main(String[] args) throws Exception { // Prepare input and screenput. BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); PrintWriter screen = new PrintWriter(System.out, true); // Create a list for simple testing. LinkedList ll = new LinkedList(); ll.addToEnd("end"); screen.println("List is " + ll); screen.println("After deleting " + ll.removeFirst() + " the list is now " + ll); ll.addToEnd("newend"); screen.println("After adding newend, the list is now " + ll); // Create a list and print it out. LinkedList lst = new LinkedList(); screen.println(lst); for (int i = 9; i > 0; i--) { Object val = new Integer(i); screen.print("Adding " + val + " to front: "); lst.addToFront(val); screen.println(lst); } for (int i = 0; i < 9; i++) { Object val = new Character((char) ('a' + i)); screen.print("Adding " + val + " to end: "); lst.addToEnd(val); screen.println(lst); } // The next two lines build and print an infinitely recursive structure // That's not a good idea. // lst.addToFront(lst); // screen.println(lst); // Remove a few lines for (int i = 0; i < 5; i++) { try { screen.println("Deleting first element ... " + lst.removeFirst()); screen.println(lst); } catch (Exception e) { screen.println("Deletion failed ... empty list."); } } // for // That's it, we're done. System.exit(0); } // main(String[]) } // class TestLinkedList