CSC 161 Grinnell College Fall, 2013
 
Imperative Problem Solving and Data Structures
 
 

Laboratory Exercise on Linked Lists for a Movie

Goals:

This laboratory helps you gain more experience with the use of lists and pointers in the context of a movie — a sequence of pictures.

Introduction

Programs in the pointers lab performed three main operations:

In the pointers lab, pictures were stored in an array. This made the display of the pictures easy; printing involved a simple for loop with an index moving over the array subscripts. However, an array has a fixed capacity, and this constraint limited the storage of pictures significantly.

In this lab, pictures are stored in a linked list:

Schematically, a movie is a sequence of nodes, and each node contains both a frame (or picture) and an indication of what frame comes next:

movie as linked list with first pointer

File scribbler-list-movie.c contains the basic elements of this linked-list-based movie program.

Steps for this Lab

  1. Copy scribbler-list-movie.c to your account, compile it, and run it. (Not much will happen, but you can check that the main program shell is syntactically correct.)

  2. Write the details for addPicture (movieList * first, Picture * frame)

    Although writing this code requires some care, you already have seen most elements of this work:

    • Refer to the cons procedure in program scheme-lists.c from the previous Scheme-like Lists lab for
      • how to use malloc to create a new node
      • how to reference a pic or data field
        (no strcpy is needed to copy a reference to a Picture — why?)
      • how to reference the next field and how to set the field to NULL.
    • Addition of the new node to the movie involves two cases:
      • If the first pointer is NULL, then the first pointer is changed to point to the new node .
      • Otherwise,
        • Follow the approach of last procedure in the previous Scheme-like Lists lab to locate the last node on the list.
        • Set the next field of the last node is set to the new node
  3. Write the details for void printForward (movieList first); which displays all pictures taken from the first to the most recent.

    • Refer to the listPrint procedure in program scheme-lists.c from the previous Scheme-like Lists lab for an appropriate interative algorithm (changing printing to the display of pictures).
  4. Write the details for function void printReverse (movieList first); which displays all pictures taken from the most recent to the first.

    • Hint: The C code can follow the same approach you used often in Scheme (think recursively).
      • Do NOT write loops for this procedure!
      • What is a simple base case?
      • How can a recursive case process something simple and call the procedure recursively to do the rest of the work?
  5. Although the algorithm given in Step 2 works, it is inefficient. In particular, the entire list of pictures must be traversed every time a new picture is added. To avoid this repeated movement through the list, an additional variable can be added to point to the last item of the list. The revised algorithm to add a picture follows:

    • Create a new node, initialize the pic to the new picture, and initialize the next field to NULL, as in part 2 of this lab (above).
    • Addition of the new node to the movie involves two cases:
      • If the first pointer is NULL, then the first pointer is changed to point to the new node , and the pointer to the last node is changed to point to the new node.
      • Otherwise,
        • Use the last pointer to identify the last node on the list.
        • Set the next field of the last node is set to the new node
        • Update the last pointer to designate the new node (now included at the end of the list).

    Schematically, a movie is a sequence of nodes, as illustrated before, but now a last pointer is included as well as the first:

    movie as linked list with first and last pointers

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.