Complete the code to initialize the front pointer of the circular queue.
private int front = [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.
public bool IsFull() {
return (rear + 1) % size == [1];
}The queue is full when the next position of rear is equal to front.
Fix the error in the Enqueue method to update rear correctly.
public void Enqueue(int item) {
if (IsFull()) {
throw new InvalidOperationException("Queue is full");
}
rear = [1];
queue[rear] = item;
if (front == -1) front = 0;
}rear must be updated using modulo to wrap around the array correctly.
Fill both blanks to correctly update front and return the dequeued item.
public int Dequeue() {
if (IsEmpty()) {
throw new InvalidOperationException("Queue is empty");
}
int item = queue[front];
if (front == rear) {
front = [1];
rear = [2];
} else {
front = (front + 1) % size;
}
return item;
}When the last element is dequeued, both front and rear reset to -1 to indicate empty queue.
Fill both blanks to implement the Peek method that returns the front item without removing it.
public int Peek() {
if ([1]()) {
throw new InvalidOperationException("Queue is empty");
}
return queue[[2]];
}Peek checks if queue is empty using IsEmpty(), then returns the item at front index.
