hi,
I have an exercise in C for my course in Software Development. I can't seem to figure this one out though...
I need to write a generic double linked list. Now I know C isn't very keen on generics, but here's what the task says:
Part 1:
"Create a linked list of struct listhdr elements by implementing the following functions. These expect as their firs argument the 'anchor' of your linked list. This is an element that simultaneously marks the start and end of the list. The next pointer points to the first element in the list, the prev pointer points to the last. In an empty list both point to the 'anchor' itself."
Part 2:Code:struct listhdr {
struct listhdr *prev, *next;
};
void linkedlist_init(struct listhdr *list);
void linkedlist_insert(struct listhdr *list, struct listhdr *item, struct listhdr *newitem);
void linkedlist_addfront(struct listhdr *list, struct listhdr *item);
void linkedlist_addtail(struct listhdr *list, struct listhdr *item);
void linkedlist_remove(struct listhdr *list, struct listhdr *item);
void linkedlist_swap(struct listhdr *list, struct listhdr *a, struct listhdr *b);
"You can use this implementation to create linked list of arbitrary structures (ie. struct mydata), without needing to modify the code from part one. This is possible by adding a field of type struct listhdr in the beginning of your own datastructure (see example). You can simply cast your structure to a struct listhdr (because the first element is a struct listhdr)"
Code:struct mydata {
struct listhdr list;
int myint;
float myfloat;
...
};
I've failed to find any info on casting structures to other struct types. Does anyone know how this works?
I've tried creating and casting the structs, but can't get it to work any way...
thx in advance