The 'safe' way would be to have something like
Code:
struct node {
struct node *next; // and prev if you want
enum nodeType type;
union {
struct A a;
struct B b;
} data;
};
But
- you don't have generic code without editing this struct.
- it wastes memory if A and B have a large size difference.
A 'generic' way is
Code:
struct node {
struct node *next; // and prev if you want
void *data; // points at struct A or struct B or ...
};
But
- you have to figure out separately what data is really pointing at.
Which isn't too hard, if you have separate lists for separate types.
- is two separate allocations (one for the node, one for the data)