QueueArray queue = new QueueArray(5); queue.enqueue(10); queue.enqueue(20); queue.enqueue(30); queue.dequeue(); queue.enqueue(40); queue.printQueue();
Remember that dequeue removes the front element and enqueue adds to the rear.
Initially, 10, 20, 30 are enqueued. Dequeue removes 10. Then 40 is enqueued. The queue now has 20, 30, 40.
Think about when the queue cannot accept more elements in a simple array implementation.
In a simple array queue, the queue is full when rear is at the last index (size - 1) and front is at 0.
public int dequeue() { if (front == -1 || front > rear) { System.out.println("Queue is empty"); } int element = queue[front]; front++; return element; }
Check what happens if front is -1 or greater than rear but method continues.
If the queue is empty, the method prints a message but still accesses queue[front], which is invalid and causes ArrayIndexOutOfBoundsException.
QueueArray queue = new QueueArray(5); queue.enqueue(5); queue.enqueue(10); queue.dequeue(); queue.enqueue(15); queue.enqueue(20); queue.dequeue(); queue.enqueue(25); queue.printQueue();
Track front and rear carefully after each operation.
After first dequeue, 5 is removed; after second dequeue, 10 is removed; enqueues add 15, 20, 25. Final queue is 15, 20, 25.
Think about what happens to the front index after many dequeues.
In a simple array queue, after dequeues, front moves forward leaving unused space at the start. Circular queue reuses this space by wrapping rear to start.
