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

  1. 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.
  2. Create a BlueJ project and add the two files you just downloaded.
  3. 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.
  4. 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.
  5. 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).
  6. 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.
  7. 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:
  8. 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.
  9. 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.
  10. Let's repeat the iterator experiment of earlier on. That is:
  11. Heads up: You will modify the MyLinkedList class in assignment 2 so that it too can produce a bona fide ListIterator.
  12. Electronically submit any code that you have modified: IteratorTester.java and MyLinkedList.java.

Last modified: 9/6/10 by Frederic Green.