Scheme-like Lists in C
Goals
This lab applies ideas of box-and-pointer representations and provides practice using Scheme and C syntax to work within lists in C.
Exercises
-
Copy program
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. -
Add a function
cadr
to this program that returns the data stored in the second element in a list. Useassert
to verify the precondition that such element exists. Since the data field for a node stores an array of characters (i.e., a string), the return type of this function should bechar *
. (In this exercise and the subsequent ones, you will want to add lines tomain
to test each of your methods.)-
First, write
cadr
using the Scheme-like functionscar
andcdr
. -
Next, rewrite
cadr
directly with C syntax (involving pointers andstruct
s—no calls tocar
andcdr
).
-
First, write
-
Add a function
count
which counts how many times a specified item appears on a list.count
should have two parameters: the list (a parameter of typeconst node_t *
) and the desired item (of typeconst char *
) for the search. In writing this code, you should use an iterative approach with a loop (rather than recursion).-
First, write
count
using Scheme-like functionscar
andcdr
. -
Next, rewrite
count
directly with C syntax (involving pointers andstruct
s).
-
First, write
-
The program
scheme-lists.c
deallocates all nodes ford
's list and also setsd
toNULL
. However,listDelete
would not affect variablesa
,b
,c
, ore
. 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 toNULL
explicitly.)-
What happens if try to print list
c
ord
immediately after the statementlistDelete(&d);
(before theNULL
assignments)? - Why do you get this result?
-
What happens if try to print list
-
Add a function
last
that returns the last item on the list.-
First, write
last
using Scheme-like functionscar
andcdr
. -
Next, rewrite
last
directly with C syntax (involving pointers andstruct
s).
-
First, write
-
Add a function
getIndex
that finds the index of the first occurence of the stringstr
in a list, returning-1
if no string in the list matchesstr
. For example, if the first node's string matches, a0
should be returned. If thecadr
matches, a1
should be returned, etc. As parameters,getIndex
should take a pointer to the list and a string to look for, and the function should return an integer.-
First, write
getIndex
using Scheme-like functionscar
andcdr
. -
Next, rewrite
getIndex
directly with C syntax (involving pointers andstruct
s).
- The list is null.
- There are no matching strings in the list.
- A matching string appears once in the list.
- A matching string appears more than once in the list.
-
First, write
For those with extra time
-
Traditionally, the function
last
, which you wrote in Exercise 5, might be calledrac
(because it's sort of a "reverse" of the Scheme functioncar
). Write a companion functionrdc
, that returns a new list with all but the last item of the original list (hence, it's a sort of "reverse" ofcdr
).Note: In order to preserve the original list, you will need to construct an entirely new set of nodes and copy their contents as you traverse the list.