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.
- 10-21-2005 #1
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!
- 10-21-2005 #2Linux 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
- 10-21-2005 #3Look up and read about "structures" (keyword 'struct')...How do you make it so you can use different data types in a linked list?
- 10-21-2005 #4Close, the C method is to use a union:
Originally Posted by ISOS
Then stick that into your linked list type:Code:typedef union test { unsigned char* name; unsigned long identifier; struct age { char years; char months; char days; char hours; }; } test_t;
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.Code:typedef struct linked_list { test_t * item; struct linked_list* next; } linked_list_t; linked_list_t* list = NULL;
Linux user #126863 - see http://linuxcounter.net/
- 10-22-2005 #5Roxoff:
Originally Posted by 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:
The typedefs are O.K., but not really needed in this 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 } linked_list; // create first list element struct linked_data *list_pointer = &linked_list;
So, then...
Look up and read about "structures" (keyword 'struct')...


Reply With Quote
