Find the answer to your Linux question:
Results 1 to 5 of 5
Hey guys, I'm new to C, and to test/hone my skills, I decided to try to implement a linked list. My only question is... How do you make it so ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942

    Implementing a linked list in C


    Hey guys, I'm new to C, and to test/hone my skills, I decided to try to implement a linked list. My only question is...

    How do you make it so you can use different data types in a linked list? Is this possible in C, or do you have to compile seperate versions for each type?

    Thanks in advance!

  2. #2
    Linux Newbie
    Join Date
    Nov 2004
    Location
    New York
    Posts
    150
    C++'s solution is templates. With C, you have to use the type-unsafe method of casting to void pointers. I don't know much more beyond that.
    \"Nifty News Fifty: When news breaks, we give you the pieces.\" - Sluggy Freelance

  3. #3
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    How do you make it so you can use different data types in a linked list?
    Look up and read about "structures" (keyword 'struct')...

  4. #4
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,893
    Quote Originally Posted by ISOS
    How do you make it so you can use different data types in a linked list?
    Look up and read about "structures" (keyword 'struct')...
    Close, the C method is to use a union:

    Code:
    typedef union test
    {
       unsigned char* name;
       unsigned long  identifier;
       struct age
       {
          char years;
          char months;
          char days;
          char hours;
       };
    } test_t;
    Then stick that into your linked list type:

    Code:
    typedef struct linked_list
    {
       test_t * item;
       struct linked_list* next;
    } linked_list_t;
    
    linked_list_t* list = NULL;
    From C++ there are more options. Templates classes can be used, or just a simple base class from which all data entries in the list are derived. You can even implement your whole list using a class if you feel like that, and it can store polymorphic types or other classes or whatever you like.
    Linux user #126863 - see http://linuxcounter.net/

  5. #5
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Quote Originally Posted by Roxoff
    Quote Originally Posted by ISOS
    How do you make it so you can use different data types in a linked list?
    Look up and read about "structures" (keyword 'struct')...
    Close, the C method is to use a union:

    Code:
    typedef union test
    {
       unsigned char* name;
       unsigned long  identifier;
       struct age
       {
          char years;
          char months;
          char days;
          char hours;
       };
    } test_t;
    Then stick that into your linked list type:

    Code:
    typedef struct linked_list
    {
       test_t * item;
       struct linked_list* next;
    } linked_list_t;
    
    linked_list_t* list = NULL;
    From C++ there are more options. Templates classes can be used, or just a simple base class from which all data entries in the list are derived. You can even implement your whole list using a class if you feel like that, and it can store polymorphic types or other classes or whatever you like.
    Roxoff:
    No, not close - exact.

    The purpose of unions is to allow variables of different data types to occupy the same data space. I do not believe that is what Javasnob is asking for or needing in this particular example. (By the way, you should re-think your union... :P )

    Javasnob:
    I believe Roxoff may have mis-read your question... And, after re-reading your question a few more times, I believe I may have mis-read it also... Did you mean that you wanted to link a collection of data variables (each link) of different data types (what I thought you meant) - or did you mean you wanted to link a single data variable (each link) - but with "interchangeable" data types (i.e., next "run" - different data type)? Or, please re-state the question...

    In any case, you'll end up using structures. This is a more correct example:

    Code:
    struct linked_data
    {
      <structure elements>  // variable declarations
      .
      .
      struct linked_data *next;  // link to next
      struct linked_data *prev;  // link to previous, if a doubly-linked list
    &#125; linked_list;  // create first list element
    
    struct linked_data *list_pointer = &linked_list;
    The typedefs are O.K., but not really needed in this example.



    So, then...

    Look up and read about "structures" (keyword 'struct')...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •