Start from the head node, visit each node, move to the next, and stop when you return to the head.
Execution Sample
DSA C
Node* current = head;
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != head);
Prints all nodes in the circular linked list starting from head until it loops back.
Execution Table
Step
Operation
Current Node Data
Pointer Changes
Visual State
1
Start at head
10
current = head (10)
10 -> 20 -> 30 -> 40 -> (back to 10)
2
Print current data
10
No pointer change
10 -> 20 -> 30 -> 40 -> (back to 10)
3
Move to next node
20
current = current->next (20)
10 -> 20 -> 30 -> 40 -> (back to 10)
4
Print current data
20
No pointer change
10 -> 20 -> 30 -> 40 -> (back to 10)
5
Move to next node
30
current = current->next (30)
10 -> 20 -> 30 -> 40 -> (back to 10)
6
Print current data
30
No pointer change
10 -> 20 -> 30 -> 40 -> (back to 10)
7
Move to next node
40
current = current->next (40)
10 -> 20 -> 30 -> 40 -> (back to 10)
8
Print current data
40
No pointer change
10 -> 20 -> 30 -> 40 -> (back to 10)
9
Move to next node
10
current = current->next (10)
10 -> 20 -> 30 -> 40 -> (back to 10)
10
Check if current == head
10
Condition true, stop traversal
Traversal complete
💡 Traversal stops when current pointer returns to head node.
Variable Tracker
Variable
Start
After Step 3
After Step 5
After Step 7
After Step 9
Final
current
head (10)
20
30
40
10
10 (stop)
Key Moments - 3 Insights
Why do we use a do-while loop instead of a while loop?
Because we must visit the head node at least once before checking if we returned to it, as shown in steps 1 and 2 where current starts at head and prints before condition check.
What happens if the list has only one node?
The traversal prints that single node once and stops immediately after moving back to head, similar to step 9 where current returns to head and traversal ends.
Why does traversal stop when current equals head again?
Because in a circular list, returning to head means we completed a full cycle, so continuing would repeat nodes endlessly, as shown in step 10 where condition current == head is true and traversal stops.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'current' after step 5?
A30
B40
C20
D10
💡 Hint
Check the 'Pointer Changes' column at step 5 in the execution table.
At which step does the traversal detect it has completed a full cycle?
AStep 9
BStep 10
CStep 8
DStep 7
💡 Hint
Look for the step where 'Check if current == head' is true in the execution table.
If the circular linked list had only one node with data 10, how many times would the data print?
ATwice
BZero times
COnce
DInfinite times
💡 Hint
Refer to the key moment about single-node list traversal.
Concept Snapshot
Traversal of Circular Linked List:
- Start at head node
- Visit and process current node
- Move to next node
- Stop when current returns to head
- Use do-while loop to ensure head is processed
- Prevent infinite loops by checking current == head
Full Transcript
Traversal of a circular linked list starts at the head node. We visit the current node and process its data, then move to the next node. This repeats until we return to the head node, indicating a full cycle. A do-while loop is used to ensure the head node is visited before checking the stopping condition. This prevents infinite loops and ensures all nodes are processed exactly once. If the list has only one node, it prints once and stops immediately after returning to head.