Bird
Raised Fist0
DSA Rustprogramming~10 mins

Traversal Forward and Backward in DSA Rust - 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?
| Yes↓
Repeat Visit
No↓
Reached Tail Node
↓
Visit Tail Node
↓
Move Back to Previous Node?
| Yes↓
Repeat Visit Backward
No↓
Traversal Complete
Start from the head node, move forward visiting each node until the tail, then move backward visiting nodes until reaching the head again.
Execution Sample
DSA Rust
let mut current = head;
while let Some(node) = current {
    print!("{} -> ", node.value);
    current = node.next.as_ref();
}
// Then backward traversal
let mut tail = tail;
while let Some(node) = tail {
    print!("{} -> ", node.value);
    tail = node.prev.as_ref();
}
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 at headNode 1current = Node 11 <-> 2 <-> 3 <-> 4 <-> 5
2Visit Node 1Node 1Print 11 <-> 2 <-> 3 <-> 4 <-> 5
3Move forwardNode 2current = Node 21 <-> 2 <-> 3 <-> 4 <-> 5
4Visit Node 2Node 2Print 21 <-> 2 <-> 3 <-> 4 <-> 5
5Move forwardNode 3current = Node 31 <-> 2 <-> 3 <-> 4 <-> 5
6Visit Node 3Node 3Print 31 <-> 2 <-> 3 <-> 4 <-> 5
7Move forwardNode 4current = Node 41 <-> 2 <-> 3 <-> 4 <-> 5
8Visit Node 4Node 4Print 41 <-> 2 <-> 3 <-> 4 <-> 5
9Move forwardNode 5current = Node 51 <-> 2 <-> 3 <-> 4 <-> 5
10Visit Node 5Node 5Print 51 <-> 2 <-> 3 <-> 4 <-> 5
11Forward traversal endsNonecurrent = None1 <-> 2 <-> 3 <-> 4 <-> 5
12Start backward at tailNode 5tail = Node 51 <-> 2 <-> 3 <-> 4 <-> 5
13Visit Node 5Node 5Print 51 <-> 2 <-> 3 <-> 4 <-> 5
14Move backwardNode 4tail = Node 41 <-> 2 <-> 3 <-> 4 <-> 5
15Visit Node 4Node 4Print 41 <-> 2 <-> 3 <-> 4 <-> 5
16Move backwardNode 3tail = Node 31 <-> 2 <-> 3 <-> 4 <-> 5
17Visit Node 3Node 3Print 31 <-> 2 <-> 3 <-> 4 <-> 5
18Move backwardNode 2tail = Node 21 <-> 2 <-> 3 <-> 4 <-> 5
19Visit Node 2Node 2Print 21 <-> 2 <-> 3 <-> 4 <-> 5
20Move backwardNode 1tail = Node 11 <-> 2 <-> 3 <-> 4 <-> 5
21Visit Node 1Node 1Print 11 <-> 2 <-> 3 <-> 4 <-> 5
22Backward traversal endsNonetail = None1 <-> 2 <-> 3 <-> 4 <-> 5
💡 Traversal stops when current or tail pointer becomes None, indicating end of list in forward or backward direction.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9After Step 11After Step 14After Step 16After Step 18After Step 20After Step 22
currentNode 1Node 2Node 3Node 4Node 5NoneNoneNoneNoneNoneNone
tailNoneNoneNoneNoneNoneNoneNode 4Node 3Node 2Node 1None
Key Moments - 3 Insights
Why does the forward traversal stop when current becomes None?
Because current points to the next node each time, when it reaches None, it means there is no next node left to visit. See execution_table row 11.
Why do we need a tail pointer for backward traversal?
Backward traversal starts from the tail node, which is the last node in the list. Without tail, we cannot move backward. See execution_table row 12.
Why do we print the node value before moving the pointer?
We print the current node's value first to visit it, then move the pointer to the next or previous node. This order ensures all nodes are visited. See execution_table rows 2,4,6,... and 13,15,17,...
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 7?
ANode 3
BNode 2
CNode 4
DNode 5
💡 Hint
Check the 'current' variable in variable_tracker after step 7.
At which step does the forward traversal end?
AStep 11
BStep 10
CStep 12
DStep 22
💡 Hint
Look for the step where 'current' becomes None in execution_table.
If the list had only 3 nodes, how would the backward traversal steps change?
ABackward traversal would not happen
BBackward traversal would start at Node 3 and end at Node 1 in fewer steps
CBackward traversal would start at Node 1
DBackward traversal would print nodes twice
💡 Hint
Think about how traversal depends on list length shown in execution_table visual state.
Concept Snapshot
Traversal Forward and Backward in Doubly Linked List:
- Start at head, visit nodes moving next until tail
- Then start at tail, visit nodes moving prev until head
- Use 'current' pointer for forward, 'tail' pointer for backward
- Stop when pointer becomes None
- Print node values during visits
Full Transcript
Traversal forward and backward in a doubly linked list means visiting each node from the start to the end, then back from the end to the start. We begin at the head node and move forward using the next pointers, printing each node's value. When we reach the end (current pointer becomes None), forward traversal stops. Then we start backward traversal from the tail node, moving backward using the prev pointers, printing each node's value until we reach the start (tail pointer becomes None). This process ensures all nodes are visited in both directions. The pointers current and tail track our position during traversal. The traversal stops when these pointers become None, indicating no more nodes to visit in that direction.

Practice

(1/5)
1. What does "Traversal Forward and Backward" mean in the context of a doubly linked list?
easy
A. Skipping nodes while moving forward
B. Moving through the list from the first node to the last and then back from the last to the first
C. Only moving from the first node to the last node
D. Moving randomly between nodes

Solution

  1. Step 1: Understand doubly linked list structure

    A doubly linked list has nodes connected both forward and backward, allowing movement in both directions.
  2. Step 2: Define traversal forward and backward

    Traversal forward means moving from the first node to the last node, and traversal backward means moving from the last node back to the first node.
  3. Final Answer:

    Moving through the list from the first node to the last and then back from the last to the first -> Option B
  4. Quick Check:

    Forward and backward traversal = Moving both ways [OK]
Hint: Think of walking to the end and then returning back [OK]
Common Mistakes:
  • Confusing traversal with skipping nodes
  • Thinking traversal is only forward
  • Assuming random movement is traversal
2. Which of the following Rust code snippets correctly traverses a doubly linked list forward using a mutable reference?
easy
A. for node in list { println!("{}", node.value); }
B. while let Some(node) = current { println!("{}", node.value); current = node.prev.as_deref_mut(); }
C. while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); }
D. loop { println!("{}", current.value); current = current.prev; }

Solution

  1. Step 1: Identify forward traversal in Rust

    Forward traversal moves from current node to next node using the next pointer.
  2. Step 2: Check code correctness

    while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); } uses current = node.next.as_deref_mut() which correctly moves forward. while let Some(node) = current { println!("{}", node.value); current = node.prev.as_deref_mut(); } moves backward, C is invalid syntax for linked list, D loops backward without condition.
  3. Final Answer:

    while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); } -> Option C
  4. Quick Check:

    Forward traversal uses next pointer [OK]
Hint: Forward traversal uses next pointer, backward uses prev [OK]
Common Mistakes:
  • Using prev pointer for forward traversal
  • Incorrect loop syntax for linked list
  • Ignoring option handling in Rust
3. Given the following Rust code for a doubly linked list traversal backward, what will be printed?
struct Node { value: i32, prev: Option, next: Option }

fn traverse_backward(mut current: Option<&Node>) {
    while let Some(node) = current {
        print!("{} ", node.value);
        current = node.prev.as_deref();
    }
}

// Assume list nodes with values 1 -> 2 -> 3, current starts at node with value 3
medium
A. 3 2 1
B. 1 2 3
C. 3 1 2
D. 1 3 2

Solution

  1. Step 1: Understand backward traversal

    Starting at node with value 3, moving backward means following prev pointers to 2, then 1.
  2. Step 2: Trace the print output

    Prints 3 first, then 2, then 1, each followed by a space.
  3. Final Answer:

    3 2 1 -> Option A
  4. Quick Check:

    Backward traversal prints nodes in reverse order [OK]
Hint: Backward traversal prints nodes from tail to head [OK]
Common Mistakes:
  • Confusing forward and backward order
  • Assuming traversal starts at head
  • Ignoring Option handling in Rust
4. Identify the error in the following Rust code snippet that attempts to traverse a doubly linked list forward:
fn traverse(mut current: Option<&Node>) {
    while current.is_some() {
        println!("{}", current.unwrap().value);
        current = current.unwrap().next.as_deref();
    }
}
medium
A. The loop condition should be while current.is_none()
B. println! macro syntax is incorrect
C. next pointer should be accessed with prev instead
D. Using unwrap() multiple times causes panic if current is None

Solution

  1. Step 1: Analyze unwrap() usage

    Calling unwrap() twice without checking can panic if current becomes None between calls.
  2. Step 2: Suggest safer approach

    Use while let Some(node) = current to safely unwrap once per iteration.
  3. Final Answer:

    Using unwrap() multiple times causes panic if current is None -> Option D
  4. Quick Check:

    Multiple unwrap() calls risk panic [OK]
Hint: Use while let Some(node) = current to avoid unwrap panic [OK]
Common Mistakes:
  • Confusing is_some() and is_none()
  • Accessing wrong pointer for forward traversal
  • Misusing println! macro
5. You have a doubly linked list with nodes containing values [10, 20, 30, 40]. Write a Rust function that traverses forward to collect values into a vector, then traverses backward from the last node to collect values into another vector. What will be the content of both vectors after traversal?
hard
A. Forward: [10, 20, 30, 40], Backward: [40, 30, 20, 10]
B. Forward: [40, 30, 20, 10], Backward: [10, 20, 30, 40]
C. Forward: [10, 20, 30], Backward: [40, 30, 20]
D. Forward: [], Backward: []

Solution

  1. Step 1: Traverse forward collecting values

    Starting at head (10), collect values in order: 10, 20, 30, 40.
  2. Step 2: Traverse backward from last node collecting values

    Starting at tail (40), collect values backward: 40, 30, 20, 10.
  3. Final Answer:

    Forward: [10, 20, 30, 40], Backward: [40, 30, 20, 10] -> Option A
  4. Quick Check:

    Forward and backward vectors match traversal order [OK]
Hint: Forward collects from head to tail, backward from tail to head [OK]
Common Mistakes:
  • Mixing order of forward and backward vectors
  • Missing last node in traversal
  • Confusing vector contents with node pointers
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