Bird
Raised Fist0
DSA C#programming~10 mins

Traversal Forward and Backward in DSA C# - Execution Trace

Choose your learning style10 modes available
Concept Flow - Traversal Forward and Backward
Start at Head Node
↓
Visit Current Node
→Move to Next Node?
↓
Repeat Until Last Node
↓
Start at Tail Node
↓
Traversal Forward Done
↓
Visit Current Node
→Move to Previous Node?
↓
Repeat Until Head Node
↓
Traversal Backward Done
Start from the head node and move forward visiting each node until the last. Then start from the tail node and move backward visiting each node until the head.
Execution Sample
DSA C#
Node current = head;
while (current != null) {
  Console.Write(current.Value + " -> ");
  current = current.Next;
}

current = tail;
while (current != null) {
  Console.Write(current.Value + " -> ");
  current = current.Prev;
}
Traverse a doubly linked list forward from head to tail, then backward from tail to head, printing node values.
Execution Table
StepOperationCurrent NodePointer ChangesVisual State
1Start forward traversalNode 1 (Head)current = head1 <-> 2 <-> 3 <-> 4 <-> 5
2Visit nodeNode 1Print 11 <-> 2 <-> 3 <-> 4 <-> 5
3Move to nextNode 2current = current.Next1 <-> 2 <-> 3 <-> 4 <-> 5
4Visit nodeNode 2Print 21 <-> 2 <-> 3 <-> 4 <-> 5
5Move to nextNode 3current = current.Next1 <-> 2 <-> 3 <-> 4 <-> 5
6Visit nodeNode 3Print 31 <-> 2 <-> 3 <-> 4 <-> 5
7Move to nextNode 4current = current.Next1 <-> 2 <-> 3 <-> 4 <-> 5
8Visit nodeNode 4Print 41 <-> 2 <-> 3 <-> 4 <-> 5
9Move to nextNode 5 (Tail)current = current.Next1 <-> 2 <-> 3 <-> 4 <-> 5
10Visit nodeNode 5Print 51 <-> 2 <-> 3 <-> 4 <-> 5
11Move to nextnullcurrent = current.Next1 <-> 2 <-> 3 <-> 4 <-> 5
12Forward traversal donenullcurrent == null1 <-> 2 <-> 3 <-> 4 <-> 5
13Start backward traversalNode 5 (Tail)current = tail1 <-> 2 <-> 3 <-> 4 <-> 5
14Visit nodeNode 5Print 51 <-> 2 <-> 3 <-> 4 <-> 5
15Move to previousNode 4current = current.Prev1 <-> 2 <-> 3 <-> 4 <-> 5
16Visit nodeNode 4Print 41 <-> 2 <-> 3 <-> 4 <-> 5
17Move to previousNode 3current = current.Prev1 <-> 2 <-> 3 <-> 4 <-> 5
18Visit nodeNode 3Print 31 <-> 2 <-> 3 <-> 4 <-> 5
19Move to previousNode 2current = current.Prev1 <-> 2 <-> 3 <-> 4 <-> 5
20Visit nodeNode 2Print 21 <-> 2 <-> 3 <-> 4 <-> 5
21Move to previousNode 1 (Head)current = current.Prev1 <-> 2 <-> 3 <-> 4 <-> 5
22Visit nodeNode 1Print 11 <-> 2 <-> 3 <-> 4 <-> 5
23Move to previousnullcurrent = current.Prev1 <-> 2 <-> 3 <-> 4 <-> 5
24Backward traversal donenullcurrent == null1 <-> 2 <-> 3 <-> 4 <-> 5
💡 Traversal stops when current becomes null after last node in forward and before first node in backward traversal.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9After Step 11After Step 15After Step 17After Step 19After Step 21After Step 23Final
currentNode 1 (Head)Node 2Node 3Node 4Node 5 (Tail)nullNode 4Node 3Node 2Node 1 (Head)nullnull
Key Moments - 3 Insights
Why does the forward traversal stop when current becomes null?
Because in the execution_table at step 11, current moves from Node 5 to null, indicating no more nodes ahead to visit.
Why do we start backward traversal from the tail node?
As shown in step 13, backward traversal begins at tail because we want to visit nodes in reverse order using the Prev pointer.
What happens if the list is empty (head is null)?
Traversal loops never start because current is null from the start, so no nodes are visited, as implied by the exit condition in step 12.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of current after step 7?
ANode 4
BNode 3
CNode 5
Dnull
💡 Hint
Check the 'Pointer Changes' and 'Current Node' columns at step 7 in the execution_table.
At which step does the forward traversal end?
AStep 11
BStep 12
CStep 24
DStep 10
💡 Hint
Look for the row where 'Forward traversal done' is noted in the 'Operation' column.
If the list had only one node, how many times would the forward traversal print a value?
A0 times
B1 time
C2 times
DDepends on the node value
💡 Hint
Refer to the execution_table steps 2 and 10 where nodes are visited and printed.
Concept Snapshot
Traversal Forward and Backward in Doubly Linked List:
- Start at head, visit nodes moving via Next pointer until null
- Then start at tail, visit nodes moving via Prev pointer until null
- Print or process each node during visits
- Stops when current pointer becomes null
- Useful to access list in both directions easily
Full Transcript
This concept shows how to traverse a doubly linked list forward from the head node to the tail node by following the Next pointers, visiting each node and printing its value. After reaching the tail, traversal continues backward from the tail to the head by following the Prev pointers, again visiting and printing each node. The traversal stops when the current pointer becomes null, indicating no more nodes in that direction. The execution table tracks each step, showing the current node, pointer changes, and the visual state of the list. The variable tracker shows how the current pointer moves through the list during forward and backward traversal. Key moments clarify why traversal stops at null and why backward traversal starts at the tail. The visual quiz tests understanding of pointer positions and traversal end conditions. This method allows easy access to nodes in both directions in a doubly linked list.

Practice

(1/5)
1. What does Traversal Forward and Backward mean in the context of a doubly linked list?
easy
A. Visiting nodes from the start to the end and then from the end back to the start
B. Only visiting nodes from the start to the end
C. Visiting nodes randomly without order
D. Visiting nodes only from the end to the start

Solution

  1. Step 1: Understand traversal directions

    Traversal forward means visiting nodes from the first node to the last node. Traversal backward means visiting nodes from the last node back to the first node.
  2. Step 2: Apply to doubly linked list

    A doubly linked list allows moving in both directions because each node has links to the next and previous nodes.
  3. Final Answer:

    Visiting nodes from the start to the end and then from the end back to the start -> Option A
  4. Quick Check:

    Forward and backward traversal = visiting nodes both ways [OK]
Hint: Think of walking forward then turning back [OK]
Common Mistakes:
  • Confusing traversal with random access
  • Thinking traversal is only forward
  • Ignoring backward links in doubly linked list
2. Which of the following is the correct syntax to traverse a doubly linked list forward in C#?
easy
A. for(Node current = tail; current != null; current = current.Next) { /* process current */ }
B. for(Node current = head; current != null; current = current.Next) { /* process current */ }
C. for(Node current = head; current != null; current = current.Prev) { /* process current */ }
D. for(Node current = tail; current != null; current = current.Prev) { /* process current */ }

Solution

  1. Step 1: Identify forward traversal

    Forward traversal starts at the head and moves to the next node repeatedly until null.
  2. Step 2: Check syntax correctness

    for(Node current = head; current != null; current = current.Next) { /* process current */ } correctly starts at head and moves using current = current.Next. Other options either start at tail or move backward.
  3. Final Answer:

    for(Node current = head; current != null; current = current.Next) { /* process current */ } -> Option B
  4. Quick Check:

    Forward traversal uses Next pointer from head [OK]
Hint: Forward means start at head and use Next [OK]
Common Mistakes:
  • Starting at tail for forward traversal
  • Using Prev pointer for forward traversal
  • Confusing Next and Prev pointers
3. Given the doubly linked list with nodes containing values 1, 2, 3, 4, what is the output of this forward traversal code?
Node current = head;
while(current != null) {
    Console.Write(current.Value + " -> ");
    current = current.Next;
}
Console.WriteLine("null");
medium
A. null
B. 4 -> 3 -> 2 -> 1 -> null
C. 1 -> 2 -> 3 -> 4 -> null
D. 1 -> 2 -> 3 -> null

Solution

  1. Step 1: Understand the list structure

    The list nodes are 1 linked to 2, then 3, then 4, in order.
  2. Step 2: Trace the forward traversal

    Starting at head (1), print 1, move to 2, print 2, move to 3, print 3, move to 4, print 4, then current becomes null and loop ends.
  3. Final Answer:

    1 -> 2 -> 3 -> 4 -> null -> Option C
  4. Quick Check:

    Forward traversal prints nodes in order [OK]
Hint: Follow Next pointers from head to null [OK]
Common Mistakes:
  • Printing backward order instead of forward
  • Missing last node in output
  • Printing null before nodes
4. Identify the error in this backward traversal code of a doubly linked list:
Node current = head;
while(current != null) {
    Console.Write(current.Value + " <- ");
    current = current.Prev;
}
Console.WriteLine("null");
medium
A. Missing null check in loop
B. Using Prev pointer instead of Next
C. Incorrect print format
D. Starting traversal from head instead of tail

Solution

  1. Step 1: Understand backward traversal

    Backward traversal should start from the tail node, not the head.
  2. Step 2: Analyze given code

    The code starts at head and tries to move backward using Prev, but head.Prev is null, so loop ends immediately.
  3. Final Answer:

    Starting traversal from head instead of tail -> Option D
  4. Quick Check:

    Backward traversal must start at tail [OK]
Hint: Backward means start at tail, not head [OK]
Common Mistakes:
  • Starting backward traversal at head
  • Confusing Prev and Next pointers
  • Ignoring list ends (null pointers)
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. Write a method to print the list forward and then backward without using extra data structures. What will be the output?
hard
A. 10 -> 20 -> 30 -> 40 -> null 40 -> 30 -> 20 -> 10 -> null
B. 40 -> 30 -> 20 -> 10 -> null 10 -> 20 -> 30 -> 40 -> null
C. 10 -> 20 -> 30 -> 40 -> null null
D. null 40 -> 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Traverse forward from head to tail

    Start at head (10), print each node's value moving via Next until null: 10 -> 20 -> 30 -> 40 -> null.
  2. Step 2: Traverse backward from tail to head

    After reaching tail (40), move backward using Prev pointer, printing: 40 -> 30 -> 20 -> 10 -> null.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> null 40 -> 30 -> 20 -> 10 -> null -> Option A
  4. Quick Check:

    Forward then backward traversal prints nodes in both directions [OK]
Hint: Print forward using Next, backward using Prev [OK]
Common Mistakes:
  • Trying to traverse backward from head
  • Printing null before nodes
  • Using extra data structures unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard