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

# Laboratory Exercise on More Singly-Linked Lists

## Goals

This laboratory provides you with more experience using lists and pointers.

## Steps for this Lab

The previous lab filled in two parts of the program ~walker/c/lists/namelist.c In this lab, you are to fill in two more menu options.

1. Write the details for function printReverse (struct Node *first), that prints the names in the nodes from the last node to the front.
Hint: The C code can follow the same approach you used often in Scheme (think recursively).

2. Before moving to the putFirst function, review the code for namelist.c carefully. Functions print, printLast, countList, and printReverse take parameters of type Node *, while functions addName and deleteName take parameters of type Node **. Give a careful statement of why two types of parameters are used in these various functions.

Note: Do NOT go on to the next part until you have written a convincing answer to this question!

3. Write the details for function putFirst (struct Node ** firstPtr) which reads the name of an item on the list and moves that item to the front of the list.

To perform this task, you first have to read in the name of the item desired. Then you need to search the list item-by-item to find the desired item. Next you need to remove that given item from its current place in the list (if it is not already first). Finally you need to insert that given item at the beginning of the list.

Since manipulation of lists is most efficient if only pointers are manipulated, your program should neither create new nodes nor dispose of existing ones. In particular, your program should not use either the malloc or free functions during the processing of putFirst.

Also, your program should respond appropriately in all cases. In particular, putFirst should print appropriate messages if either list is null or the item designated is not found on the list.

This document is available on the World Wide Web as

```     http://www.walker.cs.grinnell.edu/courses/161.sp09/labs/lab-lists-c-3.shtml
```