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

Laboratory Exercise on 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, the file actionlist.c contains a menu-driven program to maintain a list of robot actions and their corresponding names.

As written, the program contains functions to insert an action on the list (before a designated node), to delete a specified action from the list, to print the actions in the list, and to execute all the actions in order.

  1. Copy actionlist.c to your account, compile it, and run it several times to discover just what the program does.

    Make sure you turn the robot on before calling the E (execute) option.

As you may have discovered, in addition to the above features, program actionlist.c contains several stubs for additional functions, but details of these functions are not given. Your task in this lab is to implement these new functions on the menu. (Don't forget to add the prototype before main if you can write the functions at the bottom.)

  1. 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.

  2. Write the details for printLast (struct Node * first) which prints the name of the action for the last item on the list.

    To perform this task, you will want to move along the list item-by-item, until you reach the last item on the list.

  3. Write the details for function printReverse (struct Node *first), that prints the names in the actionData structures in the nodes from the last node to the front.

    • Test your program to see if it works for different cases such as a null list.

    Hint: The C code can follow the same approach you used often in Scheme (think recursively).

  4. Write a function reverseActions that executes all the actions in the list from the last node to the front. This should be very similar to printReverse except it will call the void functions stored in the actionData structs rather than print out the names.

    • Again think about different cases that might occur and test for those.'

    * Note: You will also need to add a menu option for this function in main.

Feedback Welcome

Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.