The goal of this project is help you think through the difference between and array-based way of storing information and a linked manner. This will also prepare you to think in terms of pointers, which we will be studying later this semester when we consider the C programming language.
Recall from before break how we constructed a linked-style of storage in a structure that is physically an array. The even numbered array positions held data; for each datum, the following odd array position contained the equivalent to a "next" reference: the array position of the next datum. Here's a diagram of an array containing the list 1, 3, 6, 8, 9, 16, 23. 23 is logically the last item in the list, even though it is not the last item physically in the array.
Copy the following file from the class directory:
cp /homes/tvandrun/Public/cs245/proj4/SimulatedLink.java .
Its methods are add()
, insert()
,
delete()
, and iterator()
.
The first and last are written for you.
Your assignment is to write insert()
,
and delete()
.
These methods are the only part of the code you need to modify
(besides uncommenting testing sections in the main()
method).
This project requires writing just a little bit of code, but it is very difficult. Do not take the approach of writing a hunk of code you don't fully understand and then fiddle with it until it works. You must think through the problem carefully and understand what you're writing when it's time to code.
The main()
method in the file will
step you through testing various parts.
It is important that you follow the instructions here about
the order in which to write part of the program
and that you test it as you go along.
I've also provided a method debug()
which will show you the physical state of the array,
plus the current head
and free
values.
Use them when necessary to figure out what's wrong with
your class.
Begin by making sure you understand the add()
and iterator()
methods.
Run the program as it is, so you see it working correctly.
insert()
method.The insert()
method takes a position
and a value (x
) and inserts x
after the given position.
(This class does not insure that the data are in sorted order,
like SortList
did.
However, in the main()
method,
I happen to add items to the list in such a way that
it is always sorted--this makes it easier to trace.)
The special case of inserting when the list is already full is written for you.
To write the insert()
method, first run through
a couple examples by hand.
What should it do?
Here's a basic outline:
StructureOutOfBoundsException
.
x
must be placed in the
next available free spot.
Write the code for insert()
and then uncomment checkpoint 2 to make sure it works.
delete()
methodNow, the method which takes a logical position in the list and deletes it. The special cases where the list is empty and when there is only one item in the list are handled for you.
What this method must accomplish is overwriting the position to be deleted with the physically last position and updating links appropriately.
To do all this, you will need to find three pieces of data. You will need an index to
My recommendation is first to to get pointers to
all four of these items.
Then perform the updates: overwrite the
position to be deleted, update the link,
and update free
and possibly head
.
The order in which you do these updates is tricky. There are several special situations you need to consider. What if the one being deleted itself precedes the item that is physically last? What if the one being deleted is physically last?
Implement the method, and then uncomment and test the checkpoints one at a time:
insert()
work for inserting items in the front of the list?
Checkpoint 6 checks this.
If necessary, go back and fix your insert()
method.
Please copy your file to the turn-in directory I've made:
/cslab.all/ubuntu/cs245/turnin/proj4/#####
where #####
is {daniel, gideon, alisa, cheney,
brian, erik, drewh, andreww}.
DUE: Friday, March 5, at 5:00 pm.