Complete the code to initialize the front pointer of the circular queue.
class CircularQueue { public: int front = [1]; int rear = -1; int size; int* arr; CircularQueue(int s) { size = s; arr = new int[size]; front = -1; rear = -1; } };
The front pointer is initialized to -1 to indicate the queue is empty initially.
Complete the code to check if the circular queue is full.
bool isFull() {
return ((rear + 1) % size) == [1];
}The queue is full when the next position of rear is front.
Fix the error in the enqueue function to correctly update the rear pointer.
void enqueue(int value) {
if (isFull()) {
std::cout << "Queue is full" << std::endl;
return;
}
rear = [1];
arr[rear] = value;
if (front == -1) front = 0;
}Rear must move forward circularly using modulo to wrap around the array.
Fill both blanks to correctly update the front pointer in dequeue and handle empty queue.
int dequeue() {
if (front == -1) {
std::cout << "Queue is empty" << std::endl;
return -1;
}
int value = arr[front];
if (front == rear) {
front = [1];
rear = [2];
} else {
front = (front + 1) % size;
}
return value;
}When the last element is dequeued, both front and rear reset to -1 to mark the queue empty.
Fill in the blank to implement a function that prints the current elements in the circular queue.
void display() {
if (front == -1) {
std::cout << "Queue is empty" << std::endl;
return;
}
int i = front;
while (true) {
std::cout << arr[i] << " ";
if (i == [1]) break;
i = (i + 1) % size;
}
std::cout << std::endl;
}The loop stops when the index reaches rear, printing all elements from front to rear circularly.
