📖 Scenario: Imagine you are building a simple linked list to keep track of tasks in a to-do list app. Each task is a node that holds the task name and a pointer to the next task.
🎯 Goal: You will create a basic Node structure with a pointer to the next node, set up a first node, link it to a second node, and finally print the tasks in order.
📋 What You'll Learn
Create a struct called Node with a task field of type String and a next field as an Option>
Create two nodes with exact task names: "Task 1" and "Task 2"
Link the first node's next pointer to the second node
Write a function to print all tasks starting from the first node
💡 Why This Matters
🌍 Real World
Linked lists are used in many apps to manage sequences of items like tasks, messages, or history where you need to add or remove items efficiently.
💼 Career
Understanding node structures and pointers is essential for software engineers working with low-level data structures, memory management, and building efficient algorithms.
Progress0 / 4 steps
1
Create the Node struct
Define a struct called Node with a task field of type String and a next field of type Option>.
DSA Rust
Hint
Use struct Node with two fields: task: String and next: Option>.
2
Create two nodes with tasks
Create a variable first of type Node with task set to "Task 1" and next set to None. Then create a variable second of type Node with task set to "Task 2" and next set to None.
DSA Rust
Hint
Use let first = Node { task: String::from("Task 1"), next: None } and similarly for second.
3
Link the first node to the second node
Modify the first node so that its next field points to the second node wrapped inside Some(Box::new(...)).
DSA Rust
Hint
Set next: Some(Box::new(second)) inside first node.
4
Print all tasks starting from the first node
Write a function called print_tasks that takes a reference to a Node and prints the task field. Then, if next is Some, recursively call print_tasks on the next node. Finally, call print_tasks(&first) inside main.
DSA Rust
Hint
Use println!("{}", node.task); and recursion with if let Some(ref next_node) = node.next.
Practice
(1/5)
1. What is the primary purpose of a next pointer in a singly linked list node structure?
easy
A. To indicate the size of the list
B. To store the data value of the node
C. To point to the next node in the list
D. To point to the previous node in the list
Solution
Step 1: Understand the role of pointers in linked lists
In a singly linked list, each node contains data and a pointer to the next node to maintain the sequence.
Step 2: Identify the function of the next pointer
The next pointer specifically points to the next node, enabling traversal through the list.
Final Answer:
To point to the next node in the list -> Option C
Quick Check:
next pointer = next node address [OK]
Hint: Next pointer always links to the following node [OK]
Common Mistakes:
Confusing next pointer with previous pointer
Thinking next stores data instead of address
Assuming next pointer stores list size
2. Which of the following is the correct Rust syntax to define a singly linked list node struct with a next pointer using Option and Box?
easy
A. struct Node { data: i32, next: Option }
B. struct Node { data: i32, next: &Node }
C. struct Node { data: i32, next: Box<Node> }
D. struct Node { data: i32, next: Option<&Node> }
Solution
Step 1: Recall Rust ownership and pointer rules for linked lists
Rust requires heap allocation for recursive types, so Box is used to allocate nodes on the heap. The Option type allows for a nullable pointer.
Step 2: Analyze each option's correctness
struct Node { data: i32, next: Box<Node> } lacks Option, so it cannot represent the end of the list (null). struct Node { data: i32, next: Option } correctly uses Option. struct Node { data: i32, next: &Node } uses a reference without lifetime, which is invalid. struct Node { data: i32, next: Option<&Node> } uses a reference inside Option, which is problematic for ownership.
Hint: Use Option for nullable next pointer in Rust [OK]
Common Mistakes:
Omitting Option to represent null
Using references without lifetimes
Not boxing recursive types
3. Given the following Rust code for a singly linked list node, what will be printed?
struct Node {
data: i32,
next: Option,
}
fn main() {
let node3 = Node { data: 3, next: None };
let node2 = Node { data: 2, next: Some(Box::new(node3)) };
let node1 = Node { data: 1, next: Some(Box::new(node2)) };
let mut current = &node1;
while let Some(ref next_node) = current.next {
println!("{} ->", current.data);
current = next_node;
}
println!("{} -> null", current.data);
}
medium
A. 1 -> 2 -> 3 -> null
B. 3 -> 2 -> 1 -> null
C. 1 -> 2 -> null
D. Compilation error due to ownership
Solution
Step 1: Trace the linked list construction
node1 points to node2, node2 points to node3, node3 points to None (end).
Step 2: Follow the while loop printing data
The loop prints current.data and moves to next node until next is None. It prints 1, then 2, then 3, then prints 3 -> null after loop.
Final Answer:
1 -> 2 -> 3 -> null -> Option A
Quick Check:
Traverse nodes in order: 1, 2, 3 [OK]
Hint: Follow next pointers from head to end [OK]
Common Mistakes:
Confusing order of nodes
Expecting reverse print
Thinking ownership causes compile error here
4. Identify the error in this Rust code snippet defining a linked list node:
struct Node {
data: i32,
next: Option<Node>,
}
medium
A. Data type i32 is invalid for node data
B. Missing Box to allocate next node on heap
C. Option cannot be used with structs
D. next should be a reference &Node instead
Solution
Step 1: Understand recursive struct requirements in Rust
Rust needs recursive types to use indirection like Box to know size at compile time.
Step 2: Analyze the next field type
Using Option<Node> causes infinite size because Node contains Node directly. It must be Option to compile.
Final Answer:
Missing Box to allocate next node on heap -> Option B
Quick Check:
Recursive structs need Box for next pointer [OK]
Hint: Use Box for recursive node pointers in Rust [OK]
Common Mistakes:
Omitting Box causes compile error
Using references without lifetimes
Thinking Option<Node> is enough
5. You want to design a doubly linked list node in Rust with safe ownership. Which of the following node struct definitions correctly uses pointers to represent prev and next nodes without causing ownership or borrowing errors?