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

Laboratory Exercise on Singly-Linked Lists

Goals:

This laboratory helps you gain more experience with the use of lists and pointers.

Steps for this Lab

This lab involves working with lists of data, using an existing driver program which actually constructs the lists. In particular, file ~walker/c/lists/namelist.c contains a menu-driven program to maintain a list of names.

As written, the program contains functions to insert a name on the list (before a designated node), to delete a specified name from the list, and to print the names on the list.

  1. Copy ~walker/c/lists/namelist.c to your account, compile it, and run it several times to discover just what the program does.

As you may have discovered, in addition to the above features, program namelist.c contains several stubs for additional functions, but details of these functions are not given. Your task in this lab is to fill in the pieces for two of these new functions on the menu.

  1. Write the details for function printLast (struct Node * first) which should print the data for the last item on the list. (If the list is null, the procedure should print a message to that effect instead.)

    To perform this task, you have to move along the list item-by-item until coming to the end, where the next field is NULL .

  2. Write the details for countList (struct Node * first) which counts the number of items in the list.

    To perform this task, you will want to move along the list item-by-item, counting the items as you go.


This document is available on the World Wide Web as

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

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