Consider a queue initially empty. We perform these operations in order:
- Enqueue 5
- Enqueue 10
- Dequeue
- Enqueue 15
What is the state of the queue after these operations?
Queue: front -> 5 -> 10 -> null Operations: 1. Enqueue(5) 2. Enqueue(10) 3. Dequeue() 4. Enqueue(15)
Remember, dequeue removes the front element.
After enqueue 5 and 10, queue is 5 -> 10 -> null.
Dequeue removes 5.
Enqueue 15 adds at the end.
Final queue: 10 -> 15 -> null.
Choose the statement that best explains the FIFO (First In First Out) principle as it applies to queues.
Think about a line of people waiting for service.
FIFO means the first element added to the queue is the first one removed, like people standing in line.
Given a queue implemented with an array and front and rear pointers, what error or behavior occurs if you try to dequeue when the queue is empty?
int dequeue() { if (front == -1 || front > rear) { // Queue is empty // What happens here? } int value = queue[front]; front++; return value; }
Think about accessing elements when none exist.
Dequeuing from empty queue causes underflow or invalid memory access because no elements exist to remove.
A circular queue of size 3 is empty initially. We perform:
- Enqueue 1
- Enqueue 2
- Enqueue 3
- Dequeue
- Enqueue 4
What is the queue content from front to rear?
Circular queue size = 3 Operations: 1. Enqueue(1) 2. Enqueue(2) 3. Enqueue(3) 4. Dequeue() 5. Enqueue(4)
Remember circular queue wraps rear to start after reaching end.
After enqueue 1,2,3 queue is full: 1->2->3.
Dequeue removes 1.
Enqueue 4 wraps rear to start.
Queue is 2->3->4.
Starting with an empty queue, perform these operations:
- Enqueue 7
- Enqueue 14
- Dequeue
- Enqueue 21
- Dequeue
- Dequeue
How many elements remain in the queue at the end?
Count carefully how many enqueue and dequeue operations happen.
Enqueue 7,14 → queue has 2 elements.
Dequeue removes 7 → 1 element left.
Enqueue 21 → 2 elements.
Dequeue removes 14 → 1 element.
Dequeue removes 21 → 0 elements left.
