CMPS 144L Spring 2024
Lab #7 (March 7/8): Indexed List via Link1

CMPS 144L IndexedListViaLink1 Lab

Background

Sample Dialog with IndexedListTester
Welcome to the IndexedListTester program.
Enter 'h' for a list of commands.

>c cat in the hat ate a rat
New list created...
cat in the hat ate a rat

>e 3
hat

>e 0
cat

>L
7

>x hat
3

>x rat
6

>x computer
-1

>i 6 tasty
cat in the hat ate a tasty rat

>i 0 The
The cat in the hat ate a tasty rat

>a quickly
The cat in the hat ate a tasty rat quickly

>r 1 pig
The pig in the hat ate a tasty rat quickly

>d 7
Deleted item is tasty
The pig in the hat ate a rat quickly

>t
quickly rat a ate hat the in pig The

>t
The pig in the hat ate a rat quickly

>p 2 5
in the hat

>q
Goodbye.

Provided to you is an incomplete Java source code file, IndexedListViaLink1. As usual, your job is to fill in the missing method bodies, which are marked by the comment "STUB!!".

An instance of this class represents an indexed list (or indexed sequence, if you prefer). A list (or sequence) is a collection of elements which come one after the other, in a linear fashion. (Stacks and queues are particular examples.) The term indexed is meant to indicate that here we are talking about lists each of whose elements can be referred to by its index, or position, within the list (as in first, second, third, etc.). Following the computer science convention, we will start counting at zero, so that the indexes of the elements in such a list are 0, 1, 2, etc.

Readers familiar with the java.util.List interface —or the java.util.ArrayList class that implements it— will recognize that what you are being asked to implement here is a very simplified version thereof, supporting only the most fundamental operations, including elemAt(), indexOf(), insert(), delete(), and replace(). (Our choice of method names differs somewhat from those in the List interface, mind you. But the effects are the same.)

As the name IndexedListViaLink1 suggests, instances of that class make use of instances of the class Link1 to serve as the underlying building blocks.

Note that, in Link1, both its item and next instance variables are declared to be public, meaning that you can refer to them directly in other classes. Thus, for example, in a Java class that has a variable junk of type Link1, you would use the syntax junk.item and junk.next to refer to the instance variables of the Link1 object to which junk refers/points.

Also provided is a "tester" program, IndexedListTester.java, that you should find very useful. It lets the user create a list of Strings, following which the user can display its elements, insert and delete elements, etc., in an interactive fashion. It repeatedly prompts the user to enter a command, reads it, and then carries out that command. To display a list of (examples of) commands that are supported, enter h (for "help") at the prompt. To the right is an example dialog between a user and this program.

With only the code that has been given to you, you should be able to use the tester program to create a list of Strings and to display it.


Representation Details (Dummy node)

You will notice that the lone instance variable in the IndexedListViaLink1 class is dummy, which is of type Link1. Its purpose is to store a reference/pointer to a "dummy" node whose next field refers/points to the node containing the list element at position zero. In the case of a list being empty, its dummy node's next field has value null.

Using a dummy node is a very well known technique that simplifies, to some degree, the code needed in the insert() and delete() methods. Specifically, it makes it so that neither inserting nor removing an item at position zero need be treated as a special case. (Concretely, what that means is that no if-else structure is needed in those methods.)

To illustrate (using ASCII art!), here is what the underlying representation of a list containing dog, cat, and cow would "look like":

   dummy
   +---+
   | * |
   +-+-+
     |
     |
     V 
+---+---+      +---+---+      +---+---+      +---+----+
|   | *-+----->|dog| *-+----->|cat| *-+----->|cow|null|
+---+---+      +---+---+      +---+---+      +---+----+
item next      item next      item next      item next

Note: To clarify, the instance variable is called dummy; the value of that variable is a reference/pointer to the Link1 object that serves as the "dummy node".

In order to keep the diagram simple, the item field in each node is shown as having value equal to some animal. In reality, each node's item field is a reference to an animal object, just as each node's next field is a reference to another node (which is a Link1 object).


Suggestions

There are six stubbed methods to be completed:

  1. lengthOf(): Start with this one. The body of the (zero-argument version of) display() should give you some very strong hints as to what the body of this method could look like.
  2. delete(): A key to completing this method is to make proper use of the (private utility) method getTo(). (You will notice how it is used to make the elemAt() and replace() methods very simple.)

    To delete an element, you first need to establish a reference/pointer to the node immediately preceding the one holding the element to be deleted.

  3. insert(): Using getTo() here is a very good idea. To insert an element, you first need to establish a reference/pointer to the node that is to become the new one's predecessor.
  4. indexOf(): Repeated use of getTo() will work here, but a more efficient approach would be to use logic like that in (the zero-argument version of) display(). Use of the equals() method is necessary in any case.

  5. display(): The two-argument version. If you complete this one, you may as well modify the zero-argument version of display() in the way suggested by the commented-out code that is already there.
  6. tsil(): This is a challenging method, as it requires serious pointer manipulation. Its purpose is to reverse the list, so that its elements are in the opposite of their original order.

If you have time at the end, rewrite the body of the first constructor so that, after creating the dummy node, it uses the insert() method to place the elements into the list (rather than making use of the pntr local variable). Think about the order in which the elements should be inserted. Is it first-to-last or last-to-first? Does it make any difference, in terms of running time? If so, why?


Submitting your Work

As usual, submit your completed work (the IndexedListViaLink1.java file) into the appropriate dropbox. Make sure to include, within the comments of the Java class, the names of all your team members.