For this assignment, develop examples of polymorphism and demonstrate how your languages can implement polymorphic functions, classes, and types.
Create a generic linked list class in C. First, create a LinkedList
struct that has a head pointer and a Node struct that can hold an
arbitrary pointer and a next pointer. You are free to use single- or
doubly-linked lists. Then implement the following functions.
- LinkedList *ll_create() - creates a new LinkedList struct, initalizes it, and returns it.
- void ll_push(LinkedList *l, void *data ) - adds a node to the front of the list, storing the given data in the node.
- void *ll_pop(Linked List *l ) - removes the node at the front of the list and returns the associated data.
- void ll_append(LinkedList *l, void *data ) - adds a node to the end of the list, storing the given data in the node.
- void *ll_remove( LinkedList *l, void *target, int (*compfunc)(void *, void *) ) - removes the first node in the list whose data matches target given the comparison function. The function returns the pointer to the data.
- int ll_size(LinkedList *l) - returns the size of the list.
- void ll_clear(LinkedList *l, void (*freefunc)(void *) ) - removes all of the nodes from the list, freeing the associated data using the given function.
- void ll_map( LinkedList *l, void (*mapfunc)(void *) ) - traverses the list and applies the given function to the data at each node.
You can test your linked list function using this test function. Compile using:
gcc -o clltest clltest.c yourLinkedList.c
Extend the test function so it uses a linked list with a second data type.
- Implement a LinkedList class in your other two languages, making use of some form of polymorphism to implement the data structure. Create the same set of functions as above, but adapt the data types, as necessary. If you language enables templates, use them. Create a test function that demonstrates you can create linked lists to store two different types of data.
- Generate a wiki page that shows the examples in your languages. Explain how the language makes use of polymorphism. If your language has generics or templates, explain how it is implemented. These do not need to be long or detailed explanations, but should identify the benefits and drawbacks of the implementation method.
- A perfectly acceptable extension for any assignment is to do a third language.
- Implement more than one method of creating a generic implementation of a data structure in one of your languages. Explain how the methods differ and any benefits/drawbacks.
- Make a compilable and runnable haiku in the language that helps to demonstrate the concept of polymorphism. poly.py
The writeup for this week is the assignment itself, plus the code in your chosen languages, which should be integrated with your writeup. Once you have written up your assignment, give your main page for this week the label:
Hand in your C code by either putting it on the Courses server or emailing it to the prof.