CSci 160 Lab 1: Generic Linked Lists and Iterators
Objectives
The goal behind this lab is to examine the generic implementations of linked lists, especially as regards
the behavior of iterators. The main purpose of this lab is to get you to
see these things in action (and also (hint, hint) to re-acquaint yourselves with the fabulous debugging features of BlueJ). But there is a little coding to do as well.
Exercises
- Start by downloading
IteratorTester.java
and MyLinkedList.java.
The latter is just the code from figure 3.24 in the text.
The sole purpose of IteratorTester.java is to provide
a class that will allow us to see how the iterator works in BlueJ.
- Create a BlueJ project and add the two files you just downloaded.
- In BlueJ, create a MyLinkedList object. For the type parameter, use
Integer. (You must use Integer in order for IteratorTester.java to work; sorry this was the only way I could see how to do this.) You now have a linked list of Integers.
- Add stuff to that linked list! Use the single-parameter
method
MyLinkedList.add(AnyType x), which adds items to the end of the list. Thanks to autoboxing, you can actually type in plain old int values when BlueJ asks you for the arguments. So, for example, if you add 3, 4 and then 5 (in that order), the list should contain 3, 4, 5. Check it by invoking the toString() method of MyLinkedList.
- Now let's see an iterator at work. Create an
IteratorTester object. When BlueJ asks you for an argument, give it the MyLinkedList object (most likely named "myLinked1") that you already created and that is sitting in the BlueJ "object bench" (that rectangular area in the lower left where objects hang out).
Use the pop-up menu on the iterator object to repeatedly invoke hasNext() and
next() (to see the int value returned from next(),
you will have to double-click on the Integer link in the
Method Result box).
- Before destroying the objects you've created (oops... if you've done that, you'll have to re-create them, sorry), also take some time to examine their contents. Double-click on objects in the object bench and follow links; see where they lead (you know, this is really the distant ancestor of web-surfing). Try to understand what you're seeing.
- Now, isn't it kind of weird to have a doubly linked list structure, but you can only iterate in one direction? Let's fix that! We ought to be able to start at the "end" of the list and "reverse iterate" our way to the "beginning". Follow these steps:
- Create a new inner class of
MyLinkedList called
ReverseLinkedListIterator. It should, like LinkedListIterator, be a private class. In fact, start by
copying the code from LinkedListIterator into the class definition of ReverseLinkedListIterator!
- Modify that code you just pasted in so that a
ReverseLinkedListIterator works as one would expect.
- Add a new public method to the
MyLinkedList
class, with almost the same signature as the iterator()
method, except with the name reverseIterator(). And, of course,
it should return a new ReverseLinkedListIterator.
- Modify
IteratorTester so that it invokes
reverseIterator() rather than iterator().
- Last but not least: Repeat the experiments outlined above to convince yourself that your reverse iterator works! And while you're at it, uncomment back in the main method of
MyLinkedList
(you may not have noticed it was there, at the bottom) and add code to demonstrate the working of the reverse iterator.
- We're not quite done. Let's see how the pros implement these kinds of structures; at least, let's watch the programs work. For this, download this other version of
IteratorTester, called JavaIteratorTester.java and also
JavaLinkedList.java.
- Create a new BlueJ project, include these recently downloaded files, compile, ya know, the usual. Note that the class
JavaLinkedList is empty, only inheriting everything from java.util.LinkedList. Its sole purpose is to allow you to easily create an object in BlueJ.
- Let's repeat the iterator experiment of earlier on. That is:
- Create a
JavaLinkedList object as you did before. One key difference is that now you will have to use the version of add() inherited from
the real, live, genuine Java 5 class LinkedList.
- Create a
JavaListIterator object using the
JavaLinkedList object you already created on the object
bench as an argument.
- Iterate through the
JavaLinkedList object using the
iterator. Only now note that you can iterate back and forth!
The iterator returned by the listIterator() method
is, well, a ListIterator. Such objects have not only
hasNext() and next(), but also
hasPrevious() and previous(), allowing you
to change direction anywhere you like in the middle of the list.
Try it! Note, by the way, that alternating calls to next()
and previous() return the same element.
- Heads up: You will modify the
MyLinkedList class in assignment 2 so that it too can produce a bona fide ListIterator.
- Electronically submit any code that you have modified:
IteratorTester.java and MyLinkedList.java.
Last modified: 9/6/10 by Frederic Green.