Complete the code to push an element onto the array-based stack.
fn push(stack: &mut Vec<i32>, value: i32) {
stack.[1](value);
}In Rust, Vec uses push to add elements at the end.
Complete the code to pop an element from the linked list stack.
fn pop(head: &mut Option<Box<Node>>) -> Option<i32> {
if let Some(node) = head.take() {
*head = node.next;
return Some(node.[1]);
}
None
}The linked list node stores the element in the field named value.
Fix the error in the linked list push function by completing the blank.
fn push(head: &mut Option<Box<Node>>, value: i32) {
let new_node = Box::new(Node {
value,
next: [1],
});
*head = Some(new_node);
}The new node's next should point to the current head, which is *head.
Fill both blanks to create a function that checks if the array stack is empty.
fn is_empty(stack: &Vec<i32>) -> bool {
stack.[1]() == [2]
}Check if the length of the vector is zero to know if the stack is empty.
Fill all three blanks to create a function that returns the top element of the linked list stack without removing it.
fn peek(head: &Option<Box<Node>>) -> Option<i32> {
match head {
Some(node) => Some(node.[1]),
None => [2],
}
}Return the value if the node exists, otherwise return None.
