Thread: Multiple linked lists

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Multiple linked lists

    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

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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...

    Code:
    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;
    };
    
    /* ... */
    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.html

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    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

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, once you start using glib you don't want to stop

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by zacs7 View Post
    Yes, once you start using glib you don't want to stop
    Haha, sounds like you are rather ambivalent towards all of it
    =========================================
    Everytime you segfault, you murder some part of the world

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Salem View Post
    > How do we keep track of multiple linked lists?
    Arrays, trees, other lists, etc etc

    Code:
    enum { CAR, MBIKE, MAXTYPE };
    listtype *lists[MAXTYPE];

    What is listtype *lists[MAXTYPE] ?

    That looks like something I've been looking for in google
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Can I do something like this:

    Code:
    typedef struct numbers NS;
    typedef struct list LS;
    
    struct list {
      LS *next;
    };
    
    struct numbers {
      NS *array;
      int mean;
      int mode;
      int average;
    };
    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?
    =========================================
    Everytime you segfault, you murder some part of the world

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple linked lists
    By killer in forum C++ Programming
    Replies: 8
    Last Post: 07-06-2006, 01:02 AM
  2. Multiple Linked Lists w/HeadNode
    By tofugirl in forum C Programming
    Replies: 12
    Last Post: 12-10-2005, 10:21 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Can multiple linked lists share the same structure?
    By passy in forum C Programming
    Replies: 10
    Last Post: 08-28-2003, 04:38 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM