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

Doubly-Linked Lists

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.

In your work, you should follow several main principles, including:

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


This document is available on the World Wide Web as

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

created 27 September 2001
last revised 5 April 2010
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.