Due: Monday, October 23 2017, 11:59 pm
For this part, develop an example of polymorphism and demonstrate how C can implement polymorphic functions 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, initializes 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.
For this part, develop examples of polymorphism and demonstrate how the selected languages can implement polymorphic functions, classes ,and types.
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.
The submission of this project has three components:
To check whether you've made your README file clear, you may ask yourself the following questions:
Please note that it is your responsibility to explicitly indicate the extensions you have taken. If you take the extension regarding linked list in C, please indicate explicitly in the codes and README file. If you take the extensions in selected languages, please indicate explicitly in your wiki write-up or source codes and README.
© 2017 Ying Li. Page last modified: .