Complete the code to add an element to the end of the queue.
fn enqueue(queue: &mut Vec<i32>, value: i32) {
queue.[1](value);
}The push method adds an element to the end of the vector, which represents the queue.
Complete the code to check if the queue is empty before enqueueing.
fn enqueue_if_not_empty(queue: &mut Vec<i32>, value: i32) {
if !queue.[1]() {
queue.push(value);
}
}The is_empty method returns true if the vector has no elements. We use ! to check if it is not empty.
Fix the error in the enqueue function to correctly add an element.
fn enqueue(queue: &mut Vec<i32>, value: i32) {
queue.[1](value);
}The push method correctly adds an element to the end of the vector. Using pop or remove removes elements, which is incorrect for enqueue.
Fill both blanks to create a function that enqueues only if the queue size is less than max_size.
fn enqueue_with_limit(queue: &mut Vec<i32>, value: i32, max_size: usize) {
if queue.[1]() < max_size {
queue.[2](value);
}
}We check the current length of the queue with len() and add the new value using push() if the size limit is not reached.
Fill all three blanks to create a function that enqueues a value only if it is not already in the queue and the queue is not full.
fn enqueue_unique(queue: &mut Vec<i32>, value: i32, max_size: usize) {
if !queue.[1](&value) && queue.[2]() < max_size {
queue.[3](value);
}
}The function checks if the value is not already in the queue using contains, then checks if the queue size is less than max_size using len(), and finally adds the value with push().
