review studio procedures before
Work in groups of 2-3 people. I really mean it! No more than 3 people
per group please.
Doubly Linked Lists
In lecture this week, we implemented an IntList interface
using lists with one link per item.
In this studio, you will investigate implementation of our IntList
ADT using a doubly linked list. Your work will include
design and implementation of
iterators (forwards and backwards) for the list, and a discussion that
compares your implementation with the singly linked list from lecture.
Here are the steps:
- Gather your group around one workstation. Plan on taking turns
at the keyboard.
Make a new Java project called studio8 in an eclipse workspace.
Be sure to check the box to put source and compiled files in the same folder.
- Use the Import...General archive file steps to import this zip,
which contains the interface
and class stubs you will implement in a packge called lists.
- Review the
studio prep material
on doubly linked
Note: The "null" in the sentinel nodes should really be "any integer you
like because it will be ignored". In other words, the sentinels should also
be ListItem objects.
- Examine the IntList interface provided in the zip you installed.
- Now let's start a new class in this package, DoublyLinkedListOfInts:
You should now have stubs in your new class.
- Click: File...New...Class
- Fill in the name (DoublyLinkedListOfInts)
- In the Interfaces section, Add the IntList interface.
- Be sure the Inherit abstract methods box is checked.
- Finish it up
- For each method in the interface, discuss an implementation and
sketch how it would work.
Look for opportunities to reduce one method's work to another's.
- One of the most important aspects of this design is the use
of a sentinel, as depicted in the
studio prep material .
Discuss the role of sentinels and
how they affect (simplify) your implementation.
- For testing, I've included a TestLinkedListOfObjects jUnit test you can use
or adapt to your new class, by editing it or copying it into a new jUnit test. Notice the
declarations of the interface type (IntList) as compared with the actual objects instantiated
Use the interface type as much as possible. It does not commit to an implementation
and this makes it easier to change from one implementation of the interface to another.
- The ListItem class, as given to you and as used in lecture,
had only one pointer, namely next. For the doubly linked list,
you need to augment ListItem to have two pointers: next
- Work on implementing the methods as we did in class, in the following order:
- Constructor -- like the LinkedListOfInts but it will have to initialize
the head and tail
the sentinels as well. Can those instance variables be final?
It's usually a good idea to assume initially that instance variables
could be final and then retreat from that if necessary.
DO NOT MOVE ON until you have shown your constructor to your TA so we can
be sure you see how this object is supposed to be initialized. It's important
to understand that before moving to the methods below.
- void addFirst(int item) -- be sure to think about special cases, such
as adding to an empty list.
- void addLast(int item) -- recall in the studio prep material, there is
not only a head pointer, but also a tail pointer. Try to do this operation
as efficiently and cleanly as you can.
- int size() -- remember that this returns the perceived size of the list, in terms
of how the end-user views the list. The sentinels do not count toward the size.
- int indexOf(int item) -- should return -1 if the item is not found.
Otherwise it should return the first occurence of the item in the list.
- boolean remove(int item) tries to remove the first occurence of the item, if any.
The method returns true if something was removed, false otherwise.
The LinkedListOfInts object I developed in class had 3 special cases for remove (see the
code included in this studio -- I didn't have time to go over this in class).
Try to use the sentinels to your advantage to avoid special cases as much as possible.
Strive for the cleanest, correct implementation. Talk with others and other groups about
ideas for doing this.
- Iterator<Integer> iterator() will take some thought.
Here are some ideas.
- It will not work to keep the state of the Iterator in the DoublyLinkedListOfInts
It's really important that you understand why the above is true. Discuss this amongst yourselves
and talk with a TA about it. Do not move on until your group sees this point. Think of what would
happen with two nested iterators if you had the state in the DoublyLinkedLIstOfInts object.
- You can use extra classes if you need them. Just add them into the lists package
and use them however you like.
- This method must return an instance of an object that "implements" all of
the methods in the Iterator<Integer> interface:
- boolean hasNext()
- Integer next()
- void remove() -- for this method, do nothing, just have
a stub that is empty.
- IntList reverse() returns a new list with the elements in the reverse order of
Last modified 14:29:47 CDT 21 October 2008
by Ron K. Cytron