|
|
|
A sample struct Node definition:
struct Node { int value; struct Node *next; // A reference variable ! } |
C program used to traverse a linked list:
struct Node
{
int item;
struct Node *next;
};
int main(int argc, char *argv[])
{
struct Node *first = makeList(x); // Create a linked list
struct Node *p;
/* -----------------------------------
List traversal algorithm in C
----------------------------------- */
p = first;
while ( p != NULL )
{
printf("%d ", p->item);
p = p->next; // Advances p to next node
}
printf("\n");
}
|
DEMO: /home/cs255001/demo/C/Linked-list/traverse.c
(1) Start with head (a.k.a. first)
(2) Follow the location information in the next variables in the Node "objects":