CMSC313, Computer Organization & Assembly Language Programming, Fall 2012

Project 8: Generic Linked List in C

Due: Thursday November 15, 2012, 11:59pm


The objective of this programming project is to practice working with void * pointers and function pointers.


Your assignment is to add two features to the generic linked list program discussed in class, namely searching and type selection.

Recall that the linked lists discussed in class (see Lecture 19) were polymorphic in the sense that the same linked list can have nodes which hold different data types. The linked list functions are genuinely polymorphic since there is just one set of functions that works with linked list of int, linked list of double, linked list of strings ... Adding a new type to the linked list does not require access to the source code for the linked list functions.

Each node in the linked list has a pointer to a virtual function table (borrowing from C++ parlance). This table holds pointers to functions that operate on the linked list node. There is only one table per type (not one table for each node). In the programs shown in class, the table held only two functions: one for printing the data in the node and one for deallocating the node. For this assignment, you will add two more functions to the table: comparison and cloning.

Searching in a polymorphic linked list requires a small trick. Suppose we want to search the linked list for a node that has the int value 3. Some nodes in the list might not have int data, they might even carry non-numerical data (e.g., strings or linked lists). In fact, the code you write should be compatible with linked list nodes that will be defined in the future. Fortunately, there is an easy way to determine if a linked list node compatible for comparison: just check to see if the node is pointing to the correct virtual function table!

The prototype of the search function is:

base_node *LL_Search(base_node *header, void *p) ; The intent here is that p is a pointer to a linked list node and we want search in the linked list pointed by header for a node that has the same value as the data in *p. Since p is a pointer to a linked list node, you can use it to get the address of the virtual function table for its data type. Now, for each node in the linked list you first check to see if that node points to the same virtual function table. (This is called run-time type identification.) If it does, then the two nodes are compatible and you can call a comparison function to see if the values are equal.

Where is this comparison function? In the virtual function table, of course!

The second function that you must implement have this prototype:

base_node *LL_Select_Type(base_node *header, void *p) ; The function should return a pointer to a new linked list that has all the nodes with the same type as p. For example, if the linked list has the values: 2 18.700000 57.200000 14 44 2 31 94.100000 72 Then a request to select the nodes with type int should produce a linked list with the values 2, 14, 44, 2, 31 and 72. Again, you can use the pointer to the virtual function table to determine whether a node has the same type as p. Since you have to return a new linked list, each node in the list must also be new. Thus, you need to add a "clone" function to the virtual function table. This function should make a copy of the node (deep copy) and return a pointer to the newly created node.

Implementation Notes

Turning in your program

Use the UNIX submit command on the GL system to turn in your project.

You should submit 3 files: ll.c, ll_int.c and ll_double.c. Make sure that your project compiles with the original header files ll.h, ll_int.h and ll_double.h.

The UNIX command to submit should look like:

submit cs313 proj8 ll.c ll_int.c ll_double.c

Last Modified: 8 Nov 2012 17:11:10 EST by Richard Chang
to Fall 2012 CMSC 313 Homepage