ICS 211 Homework 4

Ordered Linked Lists

This assignment requires you to re-implement Linked Lists. In particular, nodes in your linked list will be ordered such that objects are stored in alphabetical order. This makes your linked list an ordered linked list.

Each ordered linked list is generic, that is, stores objects of type E, where E is a parameter to the class. However, these linked lists also store, for each object, a string identifying the object. We refer to this string as the object's key.

Each linked list node must therefore have three fields (instead of the two in a normal linked list node): the object being stored, its key, and the next reference.

The nodes in the linked list are kept in order of increasing key value. For example, the object with key "abc" must be stored in a node that is before any node with key "xyz". Keys will be unique. Keys are compared using the String.compareToIgnoreCase method of the class String. This method returns 0 if the two strings are equal, a negative number if this string is less than the argument, and a positive number otherwise.

You must implement three classes:

The AddressList is used for testing only, and there is no need to turn it in.

The OrderedLinkedList class must provide at least the following methods:

Adding an object to the linked list must add it at the correct position.

Your address book class is not graded, so the details are up to you. I would suggest that your address book class provide a user interface that allows a user to:

You should define a private inner class (in your address class) to store whatever values you want in your address book. The values should include names and telephone numbers, but may have more information, such as email addresses.

It will probably be a good idea for you to write a toString() method for this private inner class.

The details of the user interface are up to you. At the very least, you could print a prompt and have a user enter the command (one of the strings "add", "find", or "print"), then ask for the appropriate parameters for each. Or if you prefer, you can open a window to get the input from the user and report on the results.

Sample interaction

If using prompts and commands, your program could behave as follows. The parts your program prints are in this font, the user input is in this font.

Starting program, address book is empty.
enter one of: add, find, print, quit. add
enter name to add. Hello World
enter telephone number for 'Hello World'. 1-800-555-0101

'Hello World' added to telephone book, with number 1-800-555-0101
The address book has 1 entry.
enter one of: add, find, print, quit. find
enter name to search for. hello world
'Hello World' was found, number is 1-800-555-0101
The address book has 1 entry.
enter one of: add, find, print, quit. find
enter name to search for. foo
'foo' was not found
The address book has 1 entry.
enter one of: add, find, print, quit. add
enter name to add. hello world
enter telephone number for 'hello world'. 1-800-555-0105
'hello world' added to telephone book, with number 1-800-555-0105
previous phone number for 'Hello World' was 1-800-555-0101.
The address book has 1 entry.
enter one of: add, find, print, quit. add
enter name to add. foo
enter telephone number for 'foo'. 1-800-555-0102
'foo' added to telephone book, with number 1-800-555-0101
The address book has 2 entries.
enter one of: add, find, print, quit. print
'foo'  1-800-555-0102
'Hello World'  1-800-555-0101
The address book has 2 entries.
enter one of: add, find, print, quit. quit
quit, discarding contents of address book.

Note that "Hello World", with uppercase letters, is replaced by "hello world" written with lowercase letters. Using the String.compareToIgnoreCase method, the two appear to be the same, that is, the comparison returns 0, but displaying the values shows that they are not identical.

Also note that when printing, the names are returned in alphabetical order, since the linked list is ordered.

Optional Extension

If you want to make your address book code more useful, you may extend it as follows. This is for your own personal enjoyment and use, and no extra credit will be given.

Add the following method to the Ordered Linked List class:

You may also add operations to your address book class. In particular, you can allow the user to:

This private inner class can have a constructor that creates an address book entry given a string (say, a line read from a file), and a toString() method that produces a string that can be stored in a file. This allows you to make your address book persistent.

Turning in the Assignment

You are only required to turn in the one file containing the OrderedLinkedList class and its inner KeyedNode class.