Bird
0
0
DSA Cprogramming~10 mins

Traversal Forward and Backward in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Traversal Forward and Backward
Start at head node
Visit current node
Move to next node
Is next node NULL?
NoRepeat visit
Yes
Reached end, start backward traversal
Visit current node
Move to previous node
Is previous node NULL?
NoRepeat visit
Yes
Traversal complete
Traverse nodes from head to tail using next pointers, then from tail to head using previous pointers.
Execution Sample
DSA C
Node* current = head;
while (current != NULL) {
    printf("%d\n", current->data);
    current = current->next;
}

current = tail;

while (current != NULL) {
    printf("%d\n", current->data);
    current = current->prev;
}
Traverse a doubly linked list forward from head to tail, then backward from tail to head.
Execution Table
StepOperationCurrent NodePointer ChangesVisual State
1Start forward traversalNode 1 (data=10)current = head10 <-> 20 <-> 30 <-> NULL
2Visit nodeNode 1 (10)Print 1010 <-> 20 <-> 30 <-> NULL
3Move to nextNode 2 (20)current = current->next10 <-> 20 <-> 30 <-> NULL
4Visit nodeNode 2 (20)Print 2010 <-> 20 <-> 30 <-> NULL
5Move to nextNode 3 (30)current = current->next10 <-> 20 <-> 30 <-> NULL
6Visit nodeNode 3 (30)Print 3010 <-> 20 <-> 30 <-> NULL
7Move to nextNULLcurrent = current->next10 <-> 20 <-> 30 <-> NULL
8End forward traversal, start backwardNode 3 (30)current = tail10 <-> 20 <-> 30 <-> NULL
9Visit nodeNode 3 (30)Print 3010 <-> 20 <-> 30 <-> NULL
10Move to prevNode 2 (20)current = current->prev10 <-> 20 <-> 30 <-> NULL
11Visit nodeNode 2 (20)Print 2010 <-> 20 <-> 30 <-> NULL
12Move to prevNode 1 (10)current = current->prev10 <-> 20 <-> 30 <-> NULL
13Visit nodeNode 1 (10)Print 1010 <-> 20 <-> 30 <-> NULL
14Move to prevNULLcurrent = current->prev10 <-> 20 <-> 30 <-> NULL
15Traversal completeNULLcurrent == NULL10 <-> 20 <-> 30 <-> NULL
💡 Traversal stops when current becomes NULL in both forward and backward directions.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 10After Step 12Final
currentNode 1 (10)Node 2 (20)Node 3 (30)NULLNode 2 (20)Node 1 (10)NULL
Key Moments - 3 Insights
Why does the backward traversal start at the tail node, not at NULL?
Because after forward traversal ends at NULL (step 7), we reset current to tail (step 8) to start backward traversal. This is shown in execution_table rows 7 and 8.
Why do we check current != NULL before visiting nodes?
To avoid accessing data of a NULL pointer which causes errors. The check ensures we only visit valid nodes, as seen in steps 1 and 14.
Why does the forward traversal stop when current becomes NULL?
Because NULL means no more nodes ahead. This is the termination condition shown in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of current after step 5?
ANode 2 (20)
BNULL
CNode 3 (30)
DNode 1 (10)
💡 Hint
Check the 'Current Node' column at step 5 in execution_table.
At which step does the forward traversal end?
AStep 7
BStep 8
CStep 6
DStep 14
💡 Hint
Look for when current becomes NULL in forward traversal in execution_table.
If we start backward traversal from head instead of tail, what happens?
ATraversal prints nodes forward again.
BTraversal stops immediately because head->prev is NULL.
CTraversal prints nodes in reverse order correctly.
DTraversal causes an error.
💡 Hint
Refer to pointer changes and current node in backward traversal steps in execution_table.
Concept Snapshot
Traversal Forward and Backward in Doubly Linked List:
- Start at head, move next until NULL
- Visit each node's data
- Then start at tail, move prev until NULL
- Visit each node's data backward
- Use current pointer to track position
- Stop when current is NULL
Full Transcript
This concept shows how to traverse a doubly linked list forward from the head node to the tail node by following next pointers, printing each node's data. After reaching the end (NULL), traversal continues backward from the tail to the head by following prev pointers, again printing each node's data. The traversal stops when the current pointer becomes NULL in either direction. The execution table traces each step, showing the current node, pointer changes, and the list's visual state. Key moments clarify why traversal starts backward at the tail and why NULL checks are necessary to avoid errors.