Traversing list
- C course
- Intermediate
- Lesson 72/116
- 5 min
Learn to traverse a linked list with a loop.
Traversing a list
Traversing a list means visiting each node in turn from head to end. A cursor variable starts at the head and advances one step at a time.
Example: Node *cur = head; while (cur != NULL) { printf("%d ", cur->data); cur = cur->next; }. The cur != NULL condition stops the loop when we reach the end. The cur = cur->next step moves the cursor forward. This runs in O(n) time where n is the node count. Traversal doesn't modify nodes; it only reads them.
The list_print function
Traversal becomes a list_print function. It takes the head and prints each node's data field.
Definition: void list_print(Node *head) { Node *cur = head; while (cur != NULL) { printf("%d ", cur->data); cur = cur->next; } printf("\n"); }. A space follows each value; a newline ends the loop. This keeps the output tidy. The function returns void because it computes nothing; it only prints. If the list is empty (head == NULL) the loop doesn't run and only a newline is printed.
Practice Print with list_print
Write void list_print(Node *head): start with Node *cur = head;, in a while (cur != NULL) loop do printf("%d ", cur->data); and cur = cur->next;, after the loop printf("\n");. In main prepend 30, 20, 10; call list_print(head);; then free every node.
Starter code
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *list_push_front(Node *head, int value) {
Node *n = malloc(sizeof(Node));
n->data = value;
n->next = head;
return n;
}
// list_print: with a while loop print each node's data, end with a newline
// main: prepend 30, 20, 10; call list_print; free every node
int main(void) {
// write here
return 0;
}Sample solution
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *list_push_front(Node *head, int value) {
Node *n = malloc(sizeof(Node));
n->data = value;
n->next = head;
return n;
}
void list_print(Node *head) {
Node *cur = head;
while (cur != NULL) {
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
int main(void) {
Node *head = NULL;
head = list_push_front(head, 30);
head = list_push_front(head, 20);
head = list_push_front(head, 10);
list_print(head);
while (head) { Node *t = head; head = head->next; free(t); }
return 0;
}Expected output
10 20 30
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Traversal idioms
| Term | Meaning |
|---|---|
cur = head |
init cursor |
cur != NULL |
loop condition |
cur->data |
read value |
cur = cur->next |
advance one |
2 When does the while (cur != NULL) loop stop?
- when cur is NULL Correct answer
- when cur is head
- when data is 0
- when next is not NULL
Explanation: When cur is NULL we've reached the end of the list.
3 Advance the cursor
cur = cur->____;
cur = cur->next;
Explanation: cur->next moves the cursor to the next node.