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 (if present) or an empty string if the list is null or has only one element. 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 your methods.)-
First, write
cadrusing the Scheme-like functionscarandcdr. -
Next, rewrite
cadrdirectly with C syntax (involving pointers andstructs—no calls tocarandcdr).
-
First, write
-
Add a function
countwhich counts how many times a specified item appears on a list.countshould have two parameters: the list (of typelistNode*) and the desired item (of typechar*) 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 first index of the stringstrin a list, returning-1if no string in the list matchesstr. As parameters, it should take a pointer to the list and a string to look for.getIndexshould produceindex, 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.
