[prev] 53 [next]

Iteration over Linked Lists (cont)

Standard method for scanning all elements in a linked list:

NodeT *list;  // pointer to first Node in list
NodeT *p;     // pointer to "current" Node in list

p = list;
while (p != NULL) {
	… do something with p->data 
	p = p->next;
}

// which is frequently written as

for (p = list; p != NULL; p = p->next) {
	… do something with p->data 
}