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

Scheme-like Lists in C

Goals

This lab applies ideas of box-and-pointer representations and provides practice implementing lists in C.

Background: Lists in Scheme

The reading for this lab describes box-and-pointer diagrams as a graphical model for lists.

  1. Draw box-and-pointer diagrams for each of the following lists:

    
       ((x) y z)
       (x (y z))
       ((a) b (c ()))
    

Implementing Scheme List Operations in Java

The reading also describes the implementation of lists in C and presents program ~walker/c/lists/scheme-lists.c that presents relevant C functions and several test cases.

Copy program ~walker/c/lists/scheme-lists.c to your account, compile it, and run it. This program will serve as the basis for the remaining steps of this lab. Be sure to ask about any sections you do not understand.

  1. The first part of the program contains function prototypes — function signatures that specify the function's return type and parameters. These prototypes conclude with a semicolon, indicating that the body of the functions will be defined later.

    1. Change the return type of listInit to int in the prototype, but not in definition that comes later. Recompile and note what happens. (Then change the return type back for the next steps.)

    2. Change the return type of cons to struct Node * in the prototype, but not in the later definition. Recompile and note what happens. (The typedef makes listType a synonym for struct Node *, and this experiment clarifies how such a typedef statement works.)

  2. Add a function second to this program to return returns the second element in a list (if present) or an empty stringif the list is null or has only one element. (In this and subsequent exercise, you will want to add lines to main to test your methods.)

  3. Add a function count which counts how many times a specified item appears on a list. count should have two parameters: the list (of type listType) and the desired item (of type char *) for the search. In writing this code, you should use an iterative approach.

  4. Add a function last which returns the last item on the list.

  5. The program ~walker/c/lists/scheme-lists.c deallocates all nodes for d's list and also sets d to NULL. However, listDelete would not affect variables a, b, c, or e. For these variables, the nodes have been deallocated, but the variables still refer to the old memory locations. (Thus, the program sets each of these variables to NULL explicitly.)

    1. What happens if try to print list c or d immediately the statement listDelete (&d); (before the NULL assignments?

    2. Why do you think you get this result?


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/161.sp10/labs/lab-lists-c-1.shtml

created 4 May 2000
last revised 4 April 2010
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.