Complete the code to define a doubly linked list node struct with data and pointers.
struct Node<T> {
data: T,
prev: Option<Box<Node<T>>>,
[1]: Option<Box<Node<T>>>,
}The next field points to the next node in the doubly linked list.
Complete the code to initialize a new node with given data and no links.
impl<T> Node<T> {
fn new(data: T) -> Self {
Node {
data,
prev: None,
[1]: None,
}
}
}next.The next pointer is set to None when creating a new node with no next node.
Fix the error in the method that sets the next node pointer.
impl<T> Node<T> {
fn set_next(&mut self, next_node: Box<Node<T>>) {
self.[1] = Some(next_node);
}
}prev instead of next.data.The method sets the next pointer to the given node wrapped in Some.
Fill both blanks to implement a method that returns true if the node has a next node.
impl<T> Node<T> {
fn has_next(&self) -> bool {
self.[1].[2]()
}
}is_none() which returns true if there is no next node.prev.The method checks if the next pointer is Some, meaning there is a next node.
Fill all three blanks to implement a method that returns the data of the next node if it exists.
impl<T: Clone> Node<T> {
fn next_data(&self) -> Option<T> {
self.[1].as_ref().map(|node| node.[2].[3]())
}
}prev instead of next.The method accesses the next node, then clones its data to return it safely.
