Complete the code to declare the array size for the queue.
const int MAX_SIZE = [1];The queue size is set to 100 to allocate enough space for elements.
Complete the code to initialize the front index of the queue.
int front = [1];Initializing front to -1 indicates the queue is empty initially.
Fix the error in the enqueue function to correctly update the rear index.
void enqueue(int value) {
if (rear == MAX_SIZE - 1) {
cout << "Queue is full" << endl;
return;
}
rear = rear [1] 1;
arr[rear] = value;
if (front == -1) {
front = 0;
}
}We add 1 to rear to move to the next position before inserting the new value.
Fill both blanks to correctly check if the queue is empty in the dequeue function.
bool isEmpty() {
return (front == [1] || front > [2]);
}The queue is empty if front is -1 or front has passed rear.
Fill all three blanks to correctly update front and rear in dequeue and return the dequeued value.
int dequeue() {
if (isEmpty()) {
cout << "Queue is empty" << endl;
return -1;
}
int value = arr[[1]];
if (front == rear) {
front = [2];
rear = [3];
} else {
front = front + 1;
}
return value;
}We get the value at front, then if front equals rear, reset both to -1 to mark empty queue.
