Complete the code to declare the array for the queue.
private int[] queue = new int[[1]];The queue array needs a fixed size, here 10 is used to initialize the array length.
Complete the code to check if the queue is empty.
public bool IsEmpty() { return [1] == -1; }The queue is empty when the front index is -1, meaning no elements are present.
Fix the error in the Enqueue method to add an element correctly.
public void Enqueue(int item) {
if (rear == queue.Length - 1) {
Console.WriteLine("Queue is full");
return;
}
if (front == -1) front = 0;
rear = [1];
queue[rear] = item;
}To add an element, rear index must move forward by 1 before inserting the item.
Fill both blanks to correctly remove an element from the queue.
public int Dequeue() {
if (IsEmpty()) {
Console.WriteLine("Queue is empty");
return -1;
}
int item = queue[[1]];
if (front == rear) {
front = [2];
rear = -1;
} else {
front++;
}
return item;
}The item to remove is at the front index. When the last element is removed, front resets to -1.
Fill both blanks to implement the Peek method that shows the front element without removing it.
public int Peek() {
if (IsEmpty()) {
Console.WriteLine("Queue is empty");
return [1];
}
return queue[[2]];
}Peek returns -1 if empty, otherwise returns the element at front index without removing it.
