Complete the code to initialize the front pointer in the circular queue.
public class CircularQueue { private int front; private int rear; private int size; private int[] queue; public CircularQueue(int size) { this.size = size; queue = new int[size]; [1] = -1; rear = -1; } }
The front pointer should be initialized to -1 to indicate the queue is empty initially.
Complete the condition to check if the circular queue is full.
public boolean isFull() {
return (rear + 1) % size == [1];
}The queue is full when the next position of rear is front.
Fix the error in updating rear pointer after enqueue operation.
public void enqueue(int value) {
if (isFull()) {
System.out.println("Queue is full");
return;
}
rear = [1];
queue[rear] = value;
if (front == -1) {
front = 0;
}
}Rear should move to the next position in a circular manner using modulo.
Fill both blanks to correctly update front pointer after dequeue operation and check if queue becomes empty.
public int dequeue() {
if (front == -1) {
System.out.println("Queue is empty");
return -1;
}
int value = queue[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 should reset to -1 to indicate empty queue.
Fill all three blanks to implement the display method that prints all elements in the circular queue.
public void display() {
if (front == -1) {
System.out.println("Queue is empty");
return;
}
int i = front;
while (true) {
System.out.print(queue[i] + " ");
if (i == [1]) {
break;
}
i = (i + 1) [2] [3];
}
System.out.println();
}The loop stops when i reaches rear. i increments by 1 and wraps around using modulo size.
