Objectives and Overview: Maintaining a linear collection in arrays has its advantages and disadvantages. As an alternative to arrays, 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 informally evaluate the situations in which linked lists are more and less efficient alternatives to vectors or arrays.
Start CodeWarrior and create a new project called Lab2.µ on the desktop. Copy onto the desktop a copy of the ListBuilder1.cpp program file from the CS210 (Tucker) folder and add it to your project, as shown below.
Now make and run this program to be sure that it works. A sample run of the program for the input 2 3 4 5 is shown below:
This is a simple program. It reads a series of integers as input and displays them in their original order. To do this, the program builds a linked list called theList 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.
Rewrite the ListBuilder1.cpp program that you exercised in Part 1 so that it will read any number of input integers and display them in reverse order. That is, if the input is
2 3 4 5
then the output should be
Your program file for this exercise should be named ListBuilder2yourname.cpp, and it should store the input integers as a singly linked list.
Suppose we want to add a separate function, called reverse, that will reverse the order of elements in a list. With such a function, programs like the one in Part 2 would be easier to implement. That is, insertion of the call
reverse(theList);
into the ListBuilder1.cpp program would reverse the order of the elements in the list, so that the list would be displayed in reverse order without any other program changes.
Submit your C++ 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 and 3.
You may work on the programming parts of this lab in teams of two, but all answers to the questions should be developed individually.