Complete the code to initialize the front pointer of the circular queue.
class CircularQueue { constructor(size) { this.size = size; this.queue = new Array(size); this.front = [1]; this.rear = -1; this.count = 0; } }
The front pointer starts at 0 to indicate the first position in the queue.
Complete the code to check if the circular queue is full.
isFull() {
return this.count [1] this.size;
}The queue is full when the count of elements equals the size of the queue.
Fix the error in updating the rear pointer after enqueue operation.
enqueue(element) {
if (this.isFull()) {
throw new Error('Queue is full');
}
this.rear = (this.rear [1] 1) % this.size;
this.queue[this.rear] = element;
this.count++;
}To move rear forward circularly, add 1 and take modulo size.
Fill both blanks to correctly update the front pointer after dequeue operation and decrease count.
dequeue() {
if (this.isEmpty()) {
throw new Error('Queue is empty');
}
const element = this.queue[this.front];
this.front = (this.front [1] 1) % this.size;
this.count [2] 1;
return element;
}Front moves forward by adding 1 modulo size, and count decreases by 1 after dequeue.
Fill both blanks to implement the isEmpty method correctly and initialize rear pointer.
isEmpty() {
return this.count [1] 0;
}
constructor(size) {
this.size = size;
this.queue = new Array(size);
this.front = 0;
this.rear = [2];
this.count = 0;
}Queue is empty when count equals 0. Rear pointer initializes to -1. Front pointer initializes to 0.
