CSC 161 Grinnell College Spring, 2009 Imperative Problem Solving and Data Structures

## Goals

This laboratory provides you with some experience using doubly-linked lists. The lab also highlights a process of incremental program development.

## Steps for this Lab

Recent labs (Scheme-Like Linked Lists and Singly-Linked Lists) presented the user with the following menu of options:

Options available
I - Insert a name into the list
D - Delete a name from the list
C - Count the number of items on the list
F - Move an item to the front of the list
L - Print the last item on the list
P - Print the names on the list
R - Print the names in reverse order
Q - Quit

The program then carried out the designated operations using a singly-linked list. To be more specific, file ~walker/c/lists/namelist.c contained the menu-driven main program for this work, as well as functions for the insertion and deletion of a name, and printing the names. You then added functions to count names, move an item to the front of the list, and print names in other ways.

This lab asks you to rewrite the namelist.c program, so that it maintains the names on a doubly-linked list.

1. Any program should efficiently implement the desired operations using appropriate data structures.
2. If code has been developed and tested once to determine that it works well, and if the code is not changed, then the code should still work fine.

Applying these principles to the problem at hand, the approach will be to take the program namelist.c that you developed for singly-linked lists and make the minimal changes necessary to implement a doubly-linked structure.

1. Copy program namelist.c to a file doublelist.c.

2. Change the node structure, so each node has a prev pointer to a previous node, as well as data and next fields. (For clarity, you may want to replace all occurances of node by dnode.)

3. Review the various existing functions from the old namelist.c to determine which ones require modification. Such modifications might arise for either of two reasons:
1. A doubly-linked structure may require new code to maintain the double links, or
2. The doubly-linked structure may allow significantly more efficient processing for some tasks.

Identify which functions will require change according to one or both of these criteria.

4. For functions that require change, review the function headers (or signatures) to determine what, if any, changes are needed. Update those headers as needed.

5. Progress incrementally through your program revision. The basic principle should be

1. Focusing on small pieces of code allows relatively easy testing and debugging.

To begin program revision, make changes (as needed) to addName and/or print. With just these two functions, the doubly-linked list can be constructed and tested.

6. Complete revision of the various printing functions. Again, make changes only when needed for accommodation of the doubly-linked structure or for efficiency. Otherwise, use the old code without change!

7. Complete the revision for countList, deleteName, and putFirst In each case, work on only one function at a time, and complete its testing before moving to the next function.

## Work to turn in

• Commentary for step 3.
• A program listing together with testing for the completion of the individual functions updated in steps 5, 6, and 7. Of course, you should include commentary indicating why your testing shows the program is correct.

This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/161.sp09/lab-lists-double.html