📖 Scenario: Imagine you have a list of daily temperatures recorded in a week. You want to look at these temperatures both from the start of the week to the end, and then from the end back to the start.
🎯 Goal: You will create a vector of temperatures, then write code to traverse it forward and backward, printing the temperatures in both directions.
📋 What You'll Learn
Create a vector called temperatures with the exact values: 23, 25, 22, 20, 21, 19, 24
Create a variable called length that stores the length of the temperatures vector
Use a for loop with variable i to traverse temperatures forward from index 0 to length - 1
Use a for loop with variable j to traverse temperatures backward from index length - 1 to 0
Print the temperatures in forward order separated by spaces
Print the temperatures in backward order separated by spaces
💡 Why This Matters
🌍 Real World
Traversing data forward and backward is common in many applications like analyzing time series data, browsing history, or undo-redo features.
💼 Career
Understanding how to traverse collections in both directions is a fundamental skill for software developers working with data structures and algorithms.
Progress0 / 4 steps
1
Create the temperatures vector
Create a vector called temperatures with the exact values 23, 25, 22, 20, 21, 19, 24
DSA Rust
Hint
Use let temperatures = vec![23, 25, 22, 20, 21, 19, 24]; to create the vector.
2
Create a length variable
Create a variable called length that stores the length of the temperatures vector using temperatures.len()
DSA Rust
Hint
Use let length = temperatures.len(); to get the vector length.
3
Traverse forward and backward
Use a for loop with variable i from 0 to length - 1 to traverse temperatures forward, and a for loop with variable j from length - 1 down to 0 to traverse temperatures backward. Inside each loop, print the temperature followed by a space without a newline.
DSA Rust
Hint
Use for i in 0..length and for j in (0..length).rev() loops with print!("{} ", temperatures[i]) and print!("{} ", temperatures[j]) respectively.
4
Print the final output
Print the temperatures forward and backward as two separate lines exactly as shown: 23 25 22 20 21 19 24 and 24 19 21 20 22 25 23
DSA Rust
Hint
Run the program to see the forward and backward temperatures printed on separate lines.
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
Step 1: Understand doubly linked list structure
A doubly linked list has nodes connected both forward and backward, allowing movement in both directions.
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.
Final Answer:
Moving through the list from the first node to the last and then back from the last to the first -> Option B
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
Step 1: Identify forward traversal in Rust
Forward traversal moves from current node to next node using the next pointer.
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.
Final Answer:
while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); } -> Option C
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
Step 1: Understand backward traversal
Starting at node with value 3, moving backward means following prev pointers to 2, then 1.
Step 2: Trace the print output
Prints 3 first, then 2, then 1, each followed by a space.
Final Answer:
3 2 1 -> Option A
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
Step 1: Analyze unwrap() usage
Calling unwrap() twice without checking can panic if current becomes None between calls.
Step 2: Suggest safer approach
Use while let Some(node) = current to safely unwrap once per iteration.
Final Answer:
Using unwrap() multiple times causes panic if current is None -> Option D
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?