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.cto 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
cadrto this program that returns the data stored in the second element in a list. 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 tomainto test each of your methods.)-
First, write
cadrusing the Scheme-like functionscarandcdr. -
Next, rewrite
cadrdirectly with C syntax (involving pointers andstructs—no calls tocarandcdr). -
Use
assertto verify the precondition that such element exists (that is, the list has a second item).
-
First, write
-
Add a function
countwhich counts how many times a specified item appears on a list.countshould 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
countusing Scheme-like functionscarandcdr. -
Next, rewrite
countdirectly with C syntax (involving pointers andstructs).
-
First, write
-
The program
scheme-lists.cdeallocates all nodes ford's list and also setsdtoNULL. However,listDeletewould 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 toNULLexplicitly.)-
What happens if try to print list
cordimmediately after the statementlistDelete(&d);(before theNULLassignments)? - Why do you get this result?
-
What happens if try to print list
-
Add a function
lastthat returns the last item on the list.-
First, write
lastusing Scheme-like functionscarandcdr. -
Next, rewrite
lastdirectly with C syntax (involving pointers andstructs).
-
First, write
-
Add a function
getIndexthat finds the index of the first occurence of the stringstrin a list, returning-1if no string in the list matchesstr. For example, if the first node's string matches, a0should be returned. If thecadrmatches, a1should be returned, etc. As parameters,getIndexshould take a pointer to the list and a string to look for, and the function should return an integer.-
First, write
getIndexusing Scheme-like functionscarandcdr. -
Next, rewrite
getIndexdirectly with C syntax (involving pointers andstructs).
- 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.
