Lab 7
This is a rather big lab, and we may work on it for more than 1 week.
As with previous labs, set up an appropriate directory. Get feedback
on your work from the TA (only) during the lab. And submit your work
with the command
$ submit 2031 lab7 listX.c
Remember that newer submissions overwrite older ones. We shall be leaving
this submit directory open for at least 2 weeks.
The Exercise
Recall how you approach buffered I/O using the C I/O library. There is
an opaque type called FILE. You don't really know its implementation.
What you do is define a pointer to a FILE and call a function that
creates such a FILE object and returns a pointer to it. You can then
use/manipulate the FILE object (write to it/read from it) using the returned
pointer. Code might look like this:
FILE * fp;
fp = fopen("temp", "w");
...
fprintf(fp, "Using FILE object.\n");
...
We want users to be able to create and maintain a LIST of ints in
about the same way. Code might look like this:
LIST* lp = get_list(); // get a pointer to a new, empty list
...
add(lp, 5); // add 5 to the end of the list
print_list(lp); // print the list
...
remove_at_index(lp, 3); // remove the 4th element from the list
...
The user is given the predefined LIST type and functions like these to use it,
without knowing its implementation.
Here, it is implemented as a doubly linked list, but the user need not know that.
The Files
You will need the following files to get started:
-
list.o: an object module that contains functions to
manipulate a list, but does not contain a main().
-
listX.c: contains the same functions that are in
list.o
, but with no bodies. Your main task here is to fill
in these function bodies. You should be able to change Main.c
to let you test individual functions before you have completed all of them.
-
list.h: contains the
typedef
statements
that define a LIST
and function prototypes for the functions
in both list.o
and listX.c
as well as in
Main.c
. DO NOT change this file.
-
Main.c: does some partial testing of the functions
in
list.o
. With small changes, you can replace calls to
functions in list.o
with calls to identical functions from
listX.c
.
Here's how you do that: the functions in listX.c are identical to those
in list.o, except that there is a capital 'X' at the end of each function
name. For example, list.o
has print_list(LIST * lp)
,
while listX.c
has print_listX(LIST * lp)
.
Once you have written the body to print_listX(LIST * lp)
, you
can replace some/all print_list(lp)
in
Main.c
with corresponding print_listX(lp)
statements (just stick in an 'X'). Then see if you get the same testing
results.
Doing this, you can work on one function at a time.
The compile statement
After you have downloaded the 4 files above, you should compile using the
following statement:
$ gcc -Wall list.o listX.c Main.c
And run with
$ a.out | more
Keep recompiling and retesting as you gradually complete all the functions in
listX.c
.
A Step Back
What is given to you, but you should be able to create on your own,
are all the typedef
statements in list.h
that
are necessary to define the LIST type. Look at list.h and then try
doing it on you own, without looking at list.h while you are doing this.
end of lab 7