Can multiple linked lists share the same structure?

This is a discussion on Can multiple linked lists share the same structure? within the C Programming forums, part of the General Programming Boards category; Hi, Please can you tell me if multiple linked lists can share the same structure? All of the examples i ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    9

    Can multiple linked lists share the same structure?

    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

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.
    Away.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct node
    {
        struct node *prev, *next;
        size_t datatype;
        void *data;
    };
    You could do fun things like us unions of pointers to structure types or something instead. Or unions of structures. Or...

    Templates in C++.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    Thanks,

    From the sounds of it it's not something that's usually done.

    Will have a rethink....

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    yeah, if I get what you are saying

    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;
    }
    that sets up two links list using the same struct, but does nothing with them.
    //fixed code
    Last edited by chrismiceli; 08-28-2003 at 06:47 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by chrismiceli
    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;
    }
    that sets up two links list using the same struct, but does nothing with them. [/B]
    Not it doesn't. It causes a memory leak. Watch:
    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?*/
    You just lost two structures.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    sorry, a little messed up code, well mabey a lot, but it can be done.
    //fixed code
    Last edited by chrismiceli; 08-28-2003 at 06:47 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by passy
    Thanks,

    From the sounds of it it's not something that's usually done.

    Will have a rethink....
    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:
    Code:
    struct foo {
        int datatype;
        void *data;
    };
    The simple method would be as above. You could use unions, but there would be a bit to consider:

    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.
    Hope is the first step on the road to disappointment.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by chrismiceli
    sorry, a little messed up code, well mabey a lot, but it can be done.
    //fixed code
    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. :P

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    yeah, I didn't want to do anything with the structs, just show him how to set it up w/ the same struct.

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  2. Linked Lists tutorial
    By BunkFace in forum C Programming
    Replies: 2
    Last Post: 01-04-2006, 02:59 AM
  3. Linked list of linked lists???
    By Qui in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 12:13 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21