How do we keep track of multiple linked lists?
Suppose I create a linked list that stores information about cars and another about motorcycles.
I don't understand how I can do that without declaring so many structs
How do we keep track of multiple linked lists?
Suppose I create a linked list that stores information about cars and another about motorcycles.
I don't understand how I can do that without declaring so many structs
=========================================
Everytime you segfault, you murder some part of the world
By too many, I assume you mean a new node type for every struct type?
In that case, void pointers or unions for the win...
Personally I'd use void pointers and cast back... look at how linked-lists are implemented in glib (Gtk+) and you should get it. Link: http://library.gnome.org/devel/glib/...ked-Lists.htmlCode:struct motorcycle_t { int cc; /* some random data */ }; struct car_t { int seats; /* whatever */ }; enum vehicleType_t { VT_CAR = 0, VT_MOTORCYCLE }; struct listNodeWithUnions_t { union { struct car_t car; struct motorcycle_t motorcycle; } data; enum vehicleType_t type; struct listNode_t * next; struct listNode_t * previous; }; /* OR */ struct listNodeWithVoidPointers_t { void * data; enum vehicleType_t type; struct listNode_t * next; struct listNode_t * previous; }; /* ... */
Uh oh, that looks a bit tougher than I expected, but hey, thanks for the link! There are some useful list operations in there that I needed to know.
=========================================
Everytime you segfault, you murder some part of the world
Yes, once you start using glib you don't want to stop
> How do we keep track of multiple linked lists?
Arrays, trees, other lists, etc etc
Code:enum { CAR, MBIKE, MAXTYPE }; listtype *lists[MAXTYPE];
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Can I do something like this:
What I'm essentially trying to do is make several lists of numbers, and then have a pointer to these different lists to keep track of them. Would the struct list be able to do that?Code:typedef struct numbers NS; typedef struct list LS; struct list { LS *next; }; struct numbers { NS *array; int mean; int mode; int average; };
=========================================
Everytime you segfault, you murder some part of the world
> What is listtype *lists[MAXTYPE]
An array of pointers to listtype
listtype being whatever you want it to be.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.