Computer Science 210 Lab 5: Linked Lists
Due: October 14, 1997

Objectives and Overview: Maintaining a linear collection in arrays and vectors has its advantages and disadvantages. As an alternative to arrays and vectors, the linked list is sometimes preferred because of its efficient use of storage and flexibility. For certain kinds of problems, the linked list is the preferred data structure.

The purpose of this lab is to explore the process of building and maintaining a linked list of data values, and to analyze the situations in which linked lists are more and less efficient alternatives to vectors or arrays.

Part 1 - Exercise a Linked List Building Program

Start CodeWarrior and create a new project called Lab5.µ on the desktop. Make a copy of the ListBuilder1.java program file from the CS210 (Tucker) folder and add it to your project. Also copy and add the file structure.zip (which can also be found in the CS210 (Tucker) folder) to your project, as shown below. The file structure.zip is a precompiled version of the entire library of classes in your text; using it in your project instead of the Java source programs for the classes you use will save you significant compiling and running time.

Now make and run this program to be sure that it works. A sample run of the program for the input text 'these are the times' is shown below:

This is a simple program. It reads a series of words as input and displays them in their original order. To do this, the program builds a linked list called words and then displays it. Just before the program displays the list, its internal structure looks like the diagram below:

Using the CodeWarrior Debugger as an aid, answer the following questions.

  1. Draw a picture of this list immediately before each of the first and second executions of the while statement in the program for this particular input data.
  2. Taking into account the loops that occur in this program, what is its overall complexity in terms of the total number of words, n, in the input?
  3. Replace the ListBuilder1.java program by the ListBuilder2.java program, which is also in the CS210 (Tucker) folder, and run it. Summarize the overall differences between these two programs?
  4. Taking into account both the loops and the use of methods from the SinglyLinkedList class, what is the overall complexity of the overall ListBuilder2.java program? Explain.

Part 2 - Revise the Program

Using either one of the two variations of the ListBuilder program you exercised in Part 1, expand it so that it will read an input text of any size and display the words in reverse order. That is, if the input is

These are the times

then the output should be

times
the
are
These

Your program file for this exercise should be named ListBuilderyourname.java, and it should store the words in the input text as a singly linked list.

Part 3 - Doubly Linked Lists

An advantage of doubly linked lists is that they allow efficient, O(1), access of an element at either end; the head or the tail. A disadvantage is that they are more complex to implement and require more storage for each element.
  1. What would be a simple revision for the ListBuilder2.java program so that it takes advantage of the DoublyLinkedList class instead of the SinglyLinkedList class? Make this simple revision and rerun the program to ensure that your revision works.
  2. Taking into account both the loops and the use of methods from the DoublyLinkedList class, how has the overall complexity of your revised ListBuilder2.java program changed as a result of this simple revision? Explain.

Part 4 - A Programming Exercise

Suppose we want to add a method, called reverse, to the SinglyLinkedList class that will reverse the order of elements in a list. With such a method, programs like the one in Part 2 would be easier to implement. That is, insertion of the call

words.reverse()

into the ListBuilder2 program would reverse the order of the elements in the list words, so that the list would be displayed in reverse order without any other program changes.

  1. Write this reverse method. (Optionally) test and debug this new method in the SinglyLinkedList class by rerunning this variation of the ListBuilder2 program with the call to reverse inserted.
  2. Draw a diagram of the list These are the times for each of the first two steps in your reverse method. Each of your two diagrams should be similar to the one shown at the beginning of this lab.

Lab 5 Deliverables:

Submit your Java program from Part 2 of this lab by dragging it to the Drop Box folder.

Also, hand in a hard copy listing of your program, along with your answers to the questions in Parts 1, 3, and 4.

You may work on the programming parts of this lab in teams of two, but all answers to the questions should be developed individually.