Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Rust
let new_node = Box::new(Node { data: [1], prev: None, next: None });Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' or 'node' instead of the value for the data field.
Setting data to None which is invalid for the data type.
✗ Incorrect
We create a new node with the data field set to the given value.
2fill in blank
mediumComplete the code to update the previous pointer of the old head node.
DSA Rust
if let Some(old_head) = self.head.as_mut() { old_head.prev = Some([1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting prev to None which breaks the link.
Using old_head or self.head instead of new_node.
✗ Incorrect
The previous pointer of the old head should point to the new node.
3fill in blank
hardFix the error in updating the head pointer to point to the new node.
DSA Rust
self.head = Some([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Dereferencing new_node which causes a type mismatch.
Using a reference instead of ownership.
Wrapping new_node again in Box::new causing double boxing.
✗ Incorrect
The head should be set to Some(new_node) to own the new node.
4fill in blank
hardFill both blanks to correctly link the new node and the old head.
DSA Rust
new_node.next = [1]; if let Some(ref mut next_node) = new_node.next { next_node.prev = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using None for next or prev which breaks the list.
Wrapping new_node incorrectly causing ownership errors.
✗ Incorrect
The new node's next points to the old head taken from self.head, and the old head's prev points back to the new node wrapped in Some.
5fill in blank
hardFill all three blanks to complete the insert_at_beginning function for the doubly linked list.
DSA Rust
pub fn insert_at_beginning(&mut self, value: i32) {
let mut new_node = Box::new(Node { data: [1], prev: None, next: None });
new_node.next = [2];
if let Some(ref mut next_node) = new_node.next {
next_node.prev = [3];
}
self.head = Some(new_node);
}Attempts:
3 left
💡 Hint
Common Mistakes
Not using take() causing borrowing errors.
Incorrectly wrapping new_node causing ownership issues.
Setting prev or next to None incorrectly.
✗ Incorrect
We set the new node's data to value, its next to the old head taken from self.head, and the old head's prev to point back to the new node wrapped in Some.
