>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 toned-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 the version of Link1 provided to you, both its item and next instance variables are declared to be public, meaning that you can refer to them directly in other classes. The "getter" observer methods and the "setter" mutator methods are included, too, but only for the benefit of students who are more comfortable using them rather than referring to the instance variables directly.
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 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.
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 |
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).
There are five stubbed methods to be completed:
To delete an element, you first need to establish a reference that points to the node immediately preceding the one to be deleted.
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?
As usual, submit your completed work (the AryListViaLink1.java file) into the appropriate folder (here, lab6_dir). Make sure to include, within the comments of the class, the names of all your team members. After you submit, verify that you have submitted correctly by trying to download from the folder the file that you (think you have) submitted.