Hi,
Please can you tell me if multiple linked lists can share the same structure? All of the examples i have seen only show a single list per structure.
thanks
Printable View
Hi,
Please can you tell me if multiple linked lists can share the same structure? All of the examples i have seen only show a single list per structure.
thanks
Hmmm... what exactly do you mean? I can think of a few ways that would be possible in C, and a few more that would be possible in C++ using classes.
You could do fun things like us unions of pointers to structure types or something instead. Or unions of structures. Or...Code:struct node
{
struct node *prev, *next;
size_t datatype;
void *data;
};
Templates in C++.
Quzah.
Thanks,
From the sounds of it it's not something that's usually done.
Will have a rethink....
yeah, if I get what you are saying
that sets up two links list using the same struct, but does nothing with them.Code:struct test {
char hi;
struct test *next;
};
struct test *first;
struct test *current;
struct test *first0;
struct test *current0;
int main(void) {
first = (struct test *)malloc(sizeof(struct test));
first0 = (struct test *)malloc(sizeof(struct test));
current = first;
current0 = first0;
free(first);
free(first0);
return 0;
}
//fixed code
Not it doesn't. It causes a memory leak. Watch:Quote:
Originally posted by chrismicelithat sets up two links list using the same struct, but does nothing with them. [/B]Code:struct test *first;
struct test *current;
struct test *first0;
struct test *current0;
int main(void) {
first = (struct test *)malloc(sizeof(struct test));
current = (struct test *)malloc(sizeof(struct test));
current0 = (struct test *)malloc(sizeof(struct test));
first0 = (struct test *)malloc(sizeof(struct test));
current = first;
current0 = first0;
return 0;
}
You just lost two structures.Code:first = newnode();
current = newnode();
current0 = newnode();
first0 = newnode();
current = first; /*oops what happened to what current pointed at?*/
curent0 = first0; /*oops, what happened to what current0 pointed at?*/
Quzah.
sorry, a little messed up code, well mabey a lot, but it can be done.
//fixed code
It's not a bad idea. You just either use a void pointer or a union. Then use a tag of some kind to tell you what you're dealing with. You could even cobble some macros together to handle the bulk of it:Quote:
Originally posted by passy
Thanks,
From the sounds of it it's not something that's usually done.
Will have a rethink....
The simple method would be as above. You could use unions, but there would be a bit to consider:Code:struct foo {
int datatype;
void *data;
};
1) Unions limit you to whatever you put in the union definition.
2) Void pointers give let you have anything.
Now it's a trade off. Ease of use is to use a void pointer. To make it a bit more "type safe", you add a layer of complexity by having to reference a union inside your structure.
Now both of these can be good points, or bad points. You just have to weigh them and see what it's worth. Or just redesign like it sounds like you're already planning.
Quzah.
Well kind of. It depends what you're trying to illustrate. Your current code has you freeing both allocated blocks of memory, so now none of your pointers point to anything valid. Is that what you wanted? If so, congradulations. If not. Sorry. :PQuote:
Originally posted by chrismiceli
sorry, a little messed up code, well mabey a lot, but it can be done.
//fixed code
Quzah.
yeah, I didn't want to do anything with the structs, just show him how to set it up w/ the same struct.
If you want to have one node to be placed at different locations in two linked lists, you could use a structure like this:
Code:typedef struct _NODE
{
struct _NODE* List1Next;
struct _NODE* List1Prev;
struct _NODE* List2Next;
struct _NODE* List2Prev;
DATA Data;
}NODE;