// File: TestList.java (Module 10) // // Author: Rahul Simha // Created: Nov 2, 1998 // // Starting point for Ex. 10.1: an anonymous class // implementation of an Enumeration. // The current file has the list implement the enumeration. import java.util.*; abstract class ComparableObject { public abstract String toString (); public abstract int compare (ComparableObject c); } class ListItem { ComparableObject data = null; ListItem next = null; // Constructor. public ListItem (ComparableObject obj) { data = obj; next = null; } // Accessor. public ComparableObject get_data () { return data; } } // LinkedList now implements Enumeration itself. class LinkedList implements Enumeration { ListItem front = null; ListItem rear = null; int num_items = 0; // Current number of items. // Instance method to add a data item. public void add_data (ComparableObject obj) { if (front == null) { front = new ListItem (obj); rear = front; } else { // Find the right place. ListItem temp_ptr=front, prev_ptr=front; boolean found = false; while ( (!found) && (temp_ptr != null) ) { if (temp_ptr.data.compare(obj) > 0) { found = true; break; } prev_ptr = temp_ptr; temp_ptr = temp_ptr.next; } // Now insert. if (!found) { // Insert at rear. rear.next = new ListItem (obj); rear = rear.next; } else if (temp_ptr == front) { // Insert in front. ListItem Lptr = new ListItem (obj); Lptr.next = front; front = Lptr; } else { // Insert in the middle. ListItem Lptr = new ListItem (obj); prev_ptr.next = Lptr; Lptr.next = temp_ptr; } } num_items++; } public void print_list () { ListItem list_ptr = front; System.out.println ("List: (" + num_items + " items)"); int i = 1; while (list_ptr != null) { System.out.println ("Item# " + i + ": " + list_ptr.get_data()); // Must implement toString() i++; list_ptr = list_ptr.next; } } ListItem enum_ptr; // Must implement this method. public boolean hasMoreElements () { if (enum_ptr == null) return false; else return true; } // Must implement this method. public Object nextElement() { Object obj = enum_ptr.data; enum_ptr = enum_ptr.next; return obj; } // This is needed to return an Enumeration // instance to the user. public Enumeration get_enumeration () { enum_ptr = front; return this; // Using the "this" reserved word. } } // End of class "LinkedList" // An object to use in the list: class Person extends ComparableObject { String name; String ssn; // Constructor. public Person (String name_in, String ssn_in) { name = name_in; ssn = ssn_in; } // Override toString() public String toString () { return "Person: name=" + name + ", ssn=" + ssn; } // Must implement compare public int compare (ComparableObject obj) { Person p = (Person) obj; return name.compareTo (p.name); } } // End of class "Person" // Test class. public class TestList { public static void main (String[] argv) { // Create a new list object. LinkedList L = new LinkedList (); // Create a Person instance and add it to list. L.add_data (new Person ("Terminator", "444-43-4343")); L.add_data (new Person ("James Bond", "666-65-6565")); L.add_data (new Person ("Rambo", "555-54-5454")); L.add_data (new Person ("Bruce Lee", "777-76-7676")); // Print contents via an Enumeration. Enumeration e = L.get_enumeration(); while (e.hasMoreElements()) { Person p = (Person) e.nextElement(); System.out.println (p); } } } // End of class "TestList"