// File: TestList2.java // // Author: Rahul Simha // Created: Sept 21, 1998 // // A linked list in Java - v2. // List node. class ListItem { public int data = 0; ListItem next = null; } // A separate list class. class LinkedList { // Instance variables. ListItem front = null; ListItem rear = null; // Instance method to add a data item. public void addData (int d) { if (front == null) { front = new ListItem (); front.data = d; rear = front; rear.next = null; } else { rear.next = new ListItem (); rear.next.data = d; rear.next.next = null; rear = rear.next; } } // Instance method to print the list. public void printList () { ListItem listPtr = front; int i = 1; while (listPtr != null) { System.out.println ("Item# " + i + ": " + listPtr.data); i++; listPtr = listPtr.next; } } } // End of class "LinkedList" class TestList2 { public static void main (String[] argv) { // Create a new list object. LinkedList L = new LinkedList (); // Let's add the numbers 1 to 10. for (int i=1; i <= 10; i++) { L.addData (i); // Use the add_data method. } // Print the contents. L.printList(); } } // End of class "TestList2"