Computer Science 210 Lab 2: Linked Lists
Due: February 8, 1999

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. We will spend a great deal of time in this course studying the linked list structure as it serves as the basis of many of the other data structures we will use.

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. This will essentially be a first cut look at linked lists. Later when we cover Chapter 3 in the text, we will deal with lists in much more detail.

Part 1 - Exercise a Linked List Building Program

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 (Chown) 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.

  1. Draw a picture of this list immediately after each of the first and second executions of the call to the function insert for this particular input data.
  2. Taking into account the loops in this program, can you count the number of calls to the function insert, in terms of an arbitrary number of input integers, n, in the input?
  3. What is the total number of executions of the function cout <<, also expressed as a function of the total number, n, of input integers?
  4. What is the number of memory locations used for nodes in the linked list version of this problem, for the special cases of n=10, n=100, and n=1000? What about for any n?
  5. What would have been the number of memory locations used for this problem if you had used the "expanding array" strategy that you used in Lab 1, for the cases of n=10, n=100, and n=1000? What about for any n? (Hint: look for a pattern in how the size of the memory grows. Don't spend too much time on this questions, as it is very difficult to answer in the general case)

Part 2 - Revise the Program

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

5
4
3
2

Your program file for this exercise should be named ListBuilder2yourname.cpp, and it should store the input integers as a singly linked list.

Part 3 - A Programming Exercise

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.

  1. Write this reverse function. (Optionally) test and debug it in the by rerunning this variation of your ListBuilder2.cpp program with the call to reverse inserted. For those of you familiar with recursion, it might be easiest to think of how to do this recursively.
  2. Draw a diagram of the list 2 3 4 5 for each of the first two steps in your reverse function. Each of your two diagrams should be similar to the one shown at the beginning of this lab.

Lab 2 Deliverables:

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

Don't forget to rename your program before you drop it into the drop box! Eg. ListBuilder.Chown.cpp

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