View Full Version : Linked lists...
Hinkwas
02-19-2004, 07:10 PM
Hi All,
I have a header file which contains the following ...
typedef struct objectStruct* ObjectPtr;
typedef struct objectStruct {
char name[30];
char description[50];
float price;
ObjectPtr next;
} ObjectType;
Now how do I create a linked list from this?
Thanks,
Strike
02-19-2004, 07:14 PM
Smells like homework
Hinkwas
02-19-2004, 07:39 PM
Sort of but not really...
I need to create a much bigger linked list and I need to read in from a file then create a linked list from that and heaps of other things ... this is just to help me understand linked lists better so I can then move on to a bigger project (the actual assignment)...
Will you help?
Hinkwas
02-19-2004, 08:31 PM
Should it be something like this?...
ObjectType *current, *temp;
//loop this while I still want to add an object?
{
temp = NULL;
current = (ObjectType *)malloc(sizeof(ObjectType));
current->next = temp;
temp = current;
}
Is this correct???
Thanks,
tjohnsson
02-20-2004, 01:40 AM
almost....
stuka
02-20-2004, 09:53 AM
What I'd do if I were you is to create my linked list code completely independent of the code you're showing. This will let you focus on the linked list behavior separately from the object you're handling. Since this is C, you can either use a void pointer to hold the object structure and cast it back and forth as appropriate, or you could use a typedef to identify the data type of your list. Note that if you do use the typedef solution, you can only have lists of that type in the whole program - if that's not a problem for you, then that solution is more typesafe.
On the implementation side, remember that a linked list is just a set of nodes, each of which contains and piece of data and a reference to (at minimum) the next node. There is an optional 'list' piece which holds information like size of the list and perhaps the head/tail pointers if you want to go that route (if you need to know the length of the list often, it's a good idea).
vBulletin® v3.7.0, Copyright ©2000-2010, Jelsoft Enterprises Ltd.