I believe this site has a tutoral. A linked list is built from objects that have a pointer to their same type as one [or more] of their data members. This pointer is used to point to another member of the same type, forming in effect, links of a chain.
Code:
struct node
{
struct node *next;
data_type data_name;
}
You build a list by first creating a member of this type, then make another, and have it either point to the first, have the first point to it, or both. It's best to provide an anchor point for your list, and to have this either always point to the first, or the last element in your list. Consider the following:
Code:
struct node *list;
struct ndoe *ptr;
ptr = malloc( sizeof struct node );
ptr->next = NULL;
list = ptr;
Here, we make the list anchor point to the new (first) element of our list. We make that object's 'next' pointer not point to anything. This is an easy way to tell if you are at the end of your list.
To continue, we build onto our list, by prepending to it.
Code:
ptr = malloc( sizeof struct node );
ptr->next = list;
list = ptr;
Here, we first allocate a new list. Remember now that as per above, the 'list' pointer points to the first element we made.
Next, we make the new node we just allocated's 'next' pointer point to the top of the list.
Finally, we update the list pointer so that it points to the new node.
This is one way to build a single linked list.
Quzah.