CPSC 170 Lab 8: Linked Lists

As usual, create a lab8 subdirectory for today's lab, open this document in Netscape, and start emacs.

File IntList.java contains the definitions for a linked list of integers that we discussed in class today. In addition to inner class IntNode, which holds information for a single node in the list, the following IntList methods are defined:

File IntListTest.java contains a driver that allows you to experiment with these methods, much as we did in lab 5. Save both of these files to your directory, compile and run IntListTest, and play around with it to see how it works. The do the following exercises.
  1. Replace the while loops in addToEnd and print with for loops. Test your revised methods to be sure they work. Think about which style you are most comfortable with.

  2. Add the following methods to the IntList class. For each, add an option to the driver to test it.

  3. A list of objects is a lot like a list of integers, except the value stored is an Object, not an int. Write a class ObjList that contains arbitrary objects and that has addToFront, addToEnd, removeFirst, removeLast, and print methods similar to those in IntList. Note that you won't have to write all of these again; you can just make very minor modifications to your IntList methods. Also write an ObjListTest class that creates an ObjList and puts various different kinds of objects in it (String, array, etc) and then prints it. (Don't bother with a general driver like we used for IntList.)

HAND IN: