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.
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.
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
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.
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.
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.