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 { value: [1], prev: None, next: None });Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the function like 'value' or 'data'.
✗ Incorrect
The parameter name for the value to insert is 'val', so we use it to set the node's value.
2fill in blank
mediumComplete the code to check if the list is empty by testing if head is None.
DSA Rust
if self.head.[1]() {
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_some() which returns true if there is a value.
Using non-existent methods like is_empty() or is_null().
✗ Incorrect
To check if the list is empty, we check if head is None using is_none().
3fill in blank
hardFix the error in updating the previous pointer of the new node to point to the current tail.
DSA Rust
new_node.prev = Some(Box::into_raw(self.tail.[1]()));Attempts:
3 left
💡 Hint
Common Mistakes
Using as_ref() which gives an immutable reference.
Using clone() which copies the data instead of referencing.
Using unwrap() which extracts the value but does not give a reference.
✗ Incorrect
We need a mutable reference to the tail node to set the new node's prev pointer, so use as_mut().
4fill in blank
hardFill both blanks to update the tail's next pointer and then update the list's tail to the new node.
DSA Rust
self.tail.[1] = Some(new_node); self.[2] = self.tail.as_mut().unwrap().next.as_mut();
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing head and tail pointers.
Setting prev instead of next pointer.
✗ Incorrect
We set the current tail's next pointer to the new node, then update the list's tail to that new node.
5fill in blank
hardFill all three blanks to correctly insert a new node at the end of the doubly linked list.
DSA Rust
fn insert_at_end(&mut self, val: i32) {
let mut new_node = Box::new(Node { value: [1], prev: None, next: None });
if self.head.is_none() {
self.head = Some(new_node);
self.[2] = self.head.as_mut();
} else {
new_node.prev = Some(Box::into_raw(self.tail.as_mut().unwrap()));
self.tail.as_mut().unwrap().next = Some(new_node);
self.[3] = self.tail.as_mut().unwrap().next.as_mut();
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of 'tail' to update the pointer.
Using 'value' instead of 'val' for the new node's value.
✗ Incorrect
We use 'val' to set the new node's value, and update the list's tail pointer correctly to the new node.
