Thread: Linked List question...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Linked List question...

    I'm trying to create a linked list using different types of nodes. Some nodes will have the same structure and some will have a different one (ie different # of value fields, different types of fields - int,string, char,etc.) I can't seem to find the syntax to get this to work. Honestly, I don't have a clue where to begin.

    Can anyone point me in the right direction?

    Thanks,
    Dazed & Confused

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    To my knowledge, you can't do that. A linked list is a single struct or class, you can't use seperate ones unless they are part of the linked list struct. Search google.com for linked list tutorials for more information.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Not sure if that is possible.....why not just setup a diffrent list......what are the diffrences between the nodes....data types?.....maybe a template will work......

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    I have a base class with 2 descendant classes. One of those descendant classes has 2 of it's own descendant classes. Each of the classes need a data structure to hold a set of values: first name, last name, etc.

    The thing is, each of the descendant classes has to have a structure that wil hold different data structures. All of the classes need a first name, last name holder. Some need an employee ID #, some need a client ID number (alphanumeric), etc. Since I have to be able to access all the records no matter which class, I was trying to figure out how to get them all together in a linked list.

    Does anyone have another idea? Or a better way to hold the data?

    Any help would be appreciated!

    Thanks,
    Dazed & Confused

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>All of the classes need a first name, last name holder. Some need an employee ID #, some need a client ID number (alphanumeric), etc
    Can you declare your struct like.
    Code:
    struct X{
         string firstname,lastname;
         union Y
         {int employeeID;
           int clientID;
          };
          struct X *next;
    };
    >>I'm trying to create a linked list using different types of nodes. Some nodes will have the same structure and some will have a different one (ie different # of value fields, different types of fields - int,string, char,etc.
    You can have a void pointer in C structure.That can point to any structure.So I think you can use it.
    I may be wrong
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    I don't know. Haven't used Union in anything yet.
    I'll give it a try.

    Thanks,
    Dazed & Confused

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Putting all that information in a linked list is a bad idea, especially for big databases. I suggest getting the data you need only when you need it, one structure at a time, via serialization. Not only does this save memory for other important parts of your application but backups will always be there incase of a crash or if they want to see the last database they viewed. This isn't exactly what you are looking for, but an idea you could implement in the future when your program gets larger.

    Unfortunately for link lists to work, you have to know your data type which I think you do but I think you are underestimating the power of linked lists. Any type of data can be put into them, classes, other structs, int, chars etc etc etc. you just have to know it, or use templates as chad suggested. Here's an example:

    Code:
    struct linkedlist
    {
      MyFirstClass class1;
      MySecondClass class2;
      MyThirdClass class3;
      int number;
      char* string;
      // ...etc
      linkedlist* next;
    
      //Construction/Destruction
    } linkedlist;
    As you can see, any data you want can go in them. Choose wisely and good luck.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If this is not a learning exercise, then use a standard container, like list.

    Otherwise, you can use a templated solution to hold different types of data like Boost::any does.

    Or you can have a base class with the common data and interface, then the derived classes with the special extra data. You would then hold base class pointers in your list, and access the derived functionality with virtual functions.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    So I could build the list so that each node would have every class represented? (As in your idea of the struct having a class1,class2)? And the unused fields could be set to NULL/0?

    Or that I could set the pointer of the first node (say class1) to the second node(class3)?

    If the second is correct, could you provide a syntax for setting the pointer of one type node to another one?

    Or am I missing your point altogether?

    Thanks,
    Dazed & Confused

  10. #10
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    I think you could use templates. Here's an example:

    Code:
    template <typename T, T2> //syntax may not be correct
    struct linkedlist
    {
        T class1;
        T2 class 2;
        // Whatever else..
        linkedlist* next;
    };
    
    // And to call it:
    
    linkedlist* list<MyClass1, MyClass2> = new linkedlist<MyClass1, MyClass2>;
    //Again, may not be correct, its been awhile...
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    I'll try your suggestions.

    Thanks a lot!

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Since I have to be able to access all the records no matter which class, I was trying to figure out how to get them all together in a linked list.
    What problems are you having? It seems pretty straight forward: you create a list of pointers to base class. Each pointer will be able to access the data in the particular object it points to using a virtual function declared in the base class and implemented in each of the derived classes. The unique implementation in each derived class will display all the data in that derived class.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If the second is correct, could you provide a syntax for setting the pointer of one type node to another one?

    Or am I missing your point altogether?
    All the pointers will be the same type: pointer to your base class.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Virtual functions allow you to employ "polymorphism", which means when you use a base class pointer to call a function, the function that is called is not the function in the class corresponding to the pointer type, e.g. your base class. The function that is called is in the class corresponding to the object that the pointer points to, e.g. a derived class. So, each derived class can uniquely define the virtual function to do whatever it wants, like display all it's various member variables.

    A virtual function is just a function with the same name in the base class and all the derived classes with the keyword "virtual" in front of it. "virtual" means you are requesting what is called "dynamic binding", which means when you have a base class pointer and call a function with the pointer, you want the function call to go to the object the pointer points to instead of the base class.

    So, you need to do these steps:

    1) Create a linked list of pointers to base class
    2) Declare a virtual function in your base class
    3) Declare a virtual function with the same name in each of your derived classes
    4) Define the function in your derived classes to do whatever you want.
    5) Create objects of your derived class and put them in your linked list
    6) Retrieve one of the pointers from your linked list and call the function with it. The function that executes will the version of the function in the object that the pointer points to.
    Last edited by 7stud; 11-01-2005 at 04:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM