Archived

This topic is now archived and is closed to further replies.

Linked List How do i do it ?

This topic is 5304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For the life of me I can not seem to grasp linked list I have been learning C on unix for the past couple months I have finally got a good grip on the syntax, arrays, strings, structures and most of the Posix API, I just cant seem to figure out how to write a singlar linked list adding nodes and steping through each node referencing elments if anyone here rememeber how they finally grasp the concepts of link list please share them here thanks. I think I have a good idea on why there need but I could never code one External tutorials/links will really be appreciated thanks again. I have seen dozens of tutorials but most seem to jump from pretty basic to very complex, anyone here knows a very simple easy to follow singlar linked list example. ? I know understand how to do pointers within structs I see alot of documentation reference that a linklist is better than using an array of structs ?? But I dont think I have ever done that maybe I should first figure out how to use array of structs ? is this an array of structs ? typedef struct { int age; }user_t; user_t *users[3]; <<-- I fully understand the following but not sure if this is what refs I read mean as array of structs ?

Share this post


Link to post
Share on other sites
What you have there is an array of pointers to structs. This is not the same as a linked list!


A linked list requires that each item of the struct have a pointer to the next item, thusly:

struct WhatEver
{
float val;
int thing;
WhatEver *next;
};



To traverse a linked list, you use:

WhatEver *ptr;
ptr = pointer to first item in the list
while(ptr)
{
// do stuff with ptr
ptr = ptr->next;
}


The concept is basically that each item in the list keeps track of the next item. A doubly linked list is the same thing but each item has a previous pointer and a next pointer.


Hope that made some sense

Share this post


Link to post
Share on other sites
@ApochPIQ:

Please note that the syntax you are using is C++ syntax. The following is illegal in C:

struct WhatEver
{
float val;
int thing;
WhatEver *next;
};

it should be instead:

typedef struct tagWhatEver
{
float val;
int thing;
struct tagWhatEver *next;
}
WhatEver;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by TekBoy
@ApochPIQ:

Please note that the syntax you are using is C++ syntax.

[snip]



The typdef is unnecessary. This would be legal in C:

struct WhatEver
{
float val;
int thing;
struct WhatEver *next;
};

You would just have to refer to "struct WhatEver" everywhere (so the typedef is probably a good idea, just not required to be valid C code).

M.J.

Share this post


Link to post
Share on other sites