CMPS 144L Fall 2022
Lab #7: ArrayList via Link1

Background

Sample Dialog with AryListTester
Welcome to the AryListTester 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

>g 3
hat

>g 0
cat

>L
7

>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, AryListViaLink1. Of course, your job is to fill in the missing method bodies, which are marked by the comment "STUB!!".

An instance of this class represents a list (or sequence, if you prefer) of elements that can be referred to by their positions, which are numbered starting at zero. Thus, it mimics the java.util.ArrayList class, as its name suggests. Indeed, you can view it as a stripped-down version of the ArrayList class having only the most fundamental operations, including get(), insert(), delete(), and replace().

As the class name also suggests, instances of the Link1 class serve as the underlying building blocks for the lists that are instances of AryListViaLink1.

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 points.

Also provided is a "tester" program, AryListTester.java, that you should find very useful. It lets the user create a list of Strings, to display its elements, to 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 AryListViaLink1 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 "dummy node" is the Link1 object to which dummy points/refers. End of note.

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 five 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 find(). (You will notice how it is used to make the get() and replace() methods very simple.)

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

  3. insert(): Using find() here is a very good idea. To insert an element, you first need to establish a reference that points to the node that is to become the new one's predecessor.
  4. 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.
  5. 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 AryListViaLink1.java file) into the appropriate dropbox. Make sure to include, within the comments of the Java class, the names of all your team members.