10.4.3 Queues: Enqueue, Dequeue and Circular Storage
A queue stores items so that the item waiting longest is processed first. New items enter at the rear, while removals take place at the front. This produces first in, first out behaviour.
This page develops queue operations and explains why circular storage can use a fixed-size array more effectively than a simple arrangement that repeatedly shifts every remaining item. A more detailed comparison of array implementations appears in 10.4.5 Implementing ADTs with Arrays.
By the end of this section, you should be able to:
- Describe a queue as an Abstract Data Type with front and rear access rules.
- Explain the meaning of first in, first out (FIFO).
- Apply enqueue, dequeue and peek-front operations.
- Trace the logical order of items after a sequence of queue operations.
- Explain the weakness of a shifting linear-array queue.
- Trace front and rear movement in a circular queue.
- Recognise queue underflow and overflow.
- Justify the use of a queue for an appropriate situation.
The Queue Rule
A queue has two active ends. New items join at the rear, while the item at the front is the next one available for removal. Items between these two positions retain their relative order.
Request 42 entered before the other requests, so it is the next item that a dequeue operation removes.
Enqueue, Dequeue and Peek Front
| Operation | Purpose | Effect on the queue |
|---|---|---|
| Enqueue | Add a new item. | The item becomes the new rear item. |
| Dequeue | Remove and return the front item. | The next waiting item becomes the new front. |
| Peek front | Inspect the front item without removing it. | The contents and order remain unchanged. |
| IsEmpty | Test whether no item is waiting. | Returns a Boolean result. |
| IsFull | Test whether a fixed-capacity implementation has no free slot. | Returns a Boolean result. |
Tracing Queue Operations
Start with an empty queue and apply these operations:
ENQUEUE "Scan 14"
ENQUEUE "Scan 27"
ENQUEUE "Scan 35"
DEQUEUE
ENQUEUE "Scan 46"
PEEK FRONT
| Step | Operation | Queue from front to rear | Value returned |
|---|---|---|---|
| 1 | Enqueue Scan 14 | Scan 14 | β |
| 2 | Enqueue Scan 27 | Scan 14, Scan 27 | β |
| 3 | Enqueue Scan 35 | Scan 14, Scan 27, Scan 35 | β |
| 4 | Dequeue | Scan 27, Scan 35 | Scan 14 |
| 5 | Enqueue Scan 46 | Scan 27, Scan 35, Scan 46 | β |
| 6 | Peek front | Scan 27, Scan 35, Scan 46 | Scan 27 |
A Simple Linear-Array Queue
One possible implementation stores the front item at index 0. New values are placed after the current rear item.
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Before dequeue | R42 | R57 | R63 | R81 | β | β |
| After dequeue and shift | R57 | R63 | R81 | β | β | β |
Keeping the front fixed at index 0 means every remaining item may need to move left after a dequeue. The queue still behaves correctly, but repeated shifting creates unnecessary work.
Circular Storage
A circular queue treats the final array position as if it were followed by index 0. When a dequeue frees an early slot, a later enqueue can reuse that slot after the rear reaches the end of the array.
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| Stored value | Job 64 | Job 71 | β | β | β | Job 52 | Job 58 |
| Logical role | third | rear | free | free | free | front | second |
Although the values are split across the end and beginning of the array, their logical FIFO order is: Job 52 β Job 58 β Job 64 β Job 71.
NextPosition = (CurrentPosition + 1) MOD Capacity.
The modulo operation sends the position back to 0 after the final index.
Empty and Full Queues
| Condition | Meaning | Unsafe operation | Resulting problem |
|---|---|---|---|
| Empty queue | No front item is available. | Dequeue or peek front | Underflow |
| Full fixed-capacity queue | Every storage slot is occupied. | Enqueue | Overflow |
Editing Data in a Queue
The front item is directly available for inspection or removal. An item deeper in the queue is normally not directly accessible through the queue interface.
| Requested change | Possible directly? | Explanation |
|---|---|---|
| Inspect the next item | Yes | Use peek front. |
| Remove the next item | Yes | Use dequeue. |
| Add a new waiting item | Yes | Use enqueue at the rear. |
| Edit an arbitrary middle item | Not normally | Earlier items would need to be removed first, or another structure should be used. |
| Remove the newest item first | No | That would break FIFO behaviour and suggests a stack instead. |
When Is a Queue Suitable?
| Situation | Why a queue fits |
|---|---|
| Print jobs waiting for a shared printer | Jobs should normally be processed in submission order. |
| Packets waiting in a network buffer | Earlier arrivals are forwarded before later arrivals. |
| Keyboard events awaiting processing | Events should be handled in the order they occurred. |
| Requests waiting for a service worker | The longest-waiting request should be selected next. |
| Breadth-first exploration | Earlier discovered locations are explored before later ones. |
Interactive: Queue and Circular Storage Lab
Switch between linear and circular storage. Enqueue, dequeue and peek to observe FIFO behaviour, shifting, pointer movement, wrap-around, underflow and overflow.
Common Mistakes and Misconceptions
- Confusing FIFO with LIFO.
- Enqueuing at the front instead of the rear.
- Dequeuing from the rear instead of the front.
- Saying that peek front removes an item.
- Reading a wrapped queue in ordinary index order rather than from its front pointer.
- Forgetting that a circular rear can move from the final index back to index 0.
- Assuming that every linear queue must shift data.
- Trying to dequeue or peek an empty queue.
- Trying to enqueue into a full fixed-capacity queue.
- Justifying a queue without mentioning arrival order or FIFO.
Practice
Task 1: Basic trace
Start with an empty queue and apply:
ENQUEUE 18, ENQUEUE 42, ENQUEUE 27,
DEQUEUE, ENQUEUE 63.
- State the final queue from front to rear.
- State the value returned by dequeue.
- State the final front and rear items.
Task 2: Peek or dequeue?
A system must display the next print job without removing it. Name the operation and explain why it is appropriate.
Task 3: Read a wrapped queue
A circular queue of capacity 6 has its front at index 4 and rear at index 1. The occupied values are:
- index 4: P
- index 5: Q
- index 0: R
- index 1: S
- Write the queue from front to rear.
- Which item is dequeued next?
- At which index should the next item be enqueued?
Task 4: Choose and justify
Decide whether a queue is suitable for each situation.
- Processing support requests in arrival order.
- Undoing the newest drawing action.
- Forwarding network packets in arrival order.
- Editing any waiting item directly by its numbered position.
Task 5: Underflow and overflow
A fixed-capacity queue can store five values.
- What happens if dequeue is requested when the queue is empty?
- What happens if enqueue is requested when five values are already stored?
- Name both error conditions.
Review
| Question | Strong answer should include |
|---|---|
| How does a queue organise access? | Items enter at the rear and leave from the front. |
| What does FIFO mean? | The earliest item added is removed first. |
| What does enqueue do? | Adds a new item at the rear. |
| What does dequeue do? | Removes and returns the front item. |
| What does peek front do? | Returns or inspects the front item without removing it. |
| Why use circular storage? | To reuse freed array slots and avoid shifting all remaining values. |
| How is a wrapped queue read? | Begin at the front pointer and follow positions circularly to the rear. |
| How should a queue choice be justified? | By explaining that items must be processed in arrival order. |