Data Structures · Queues
Drawback of Linear Array Queue Implementation
Overview
A linear array queue stores elements in a fixed-size array using two indices, front and rear, both of which move forward as elements are deleted and inserted.
Because the two ends move in the same direction, space freed at the front can never be reused once rear reaches the end of the array, so this implementation wastes memory and is rarely used.
Key concepts
- Linear array queue: a queue implemented on a fixed-size array with two indices.
- front: points to the position from which deletion (dequeue) happens.
- rear: points to the position where insertion (enqueue) happens.
- Insertion always increments rear; deletion always increments front. Both indices move in the same direction (forward) through the array.
- Stack contrast: in a stack, the array grows and shrinks at the same end (top), so any remaining free space is always usable.
- The drawback: in a queue, insertion happens at the rear and deletion at the front, and both move forward. At some point free spaces exist in the array but cannot be used.
- Once rear reaches the last index, the queue is treated as full even if free slots exist at the front.
- Because of this wasted space, the linear-array implementation is rarely used for a queue; the fix is to treat the array as circular.
How it works
- Rear is incremented and D is inserted.
- Rear is incremented and E is inserted.
- No more insertions are possible: the queue is treated as full because rear has reached the end of the array.
- An element is deleted. Deletion always happens from the front, so the element at front is removed and front is incremented to point to the next element.
- A free space is now available at the beginning of the array, but rear cannot increment further, so that space cannot be used.
Worked examples
Array of size 5, initially front = 0, rear = -1 (empty).
| Step | Operation | front | rear | Array state |
|---|---|---|---|---|
| 1 | enqueue D | 0 | 0 | D _ _ _ _ |
| 2 | enqueue E | 0 | 1 | D E _ _ _ |
| 3 | (rear at last index) | 0 | 1 | D E _ _ _ → queue considered FULL, no more insertions |
| 4 | dequeue (from front) | 1 | 1 | _ E _ _ _ → one free space at index 0 |
| 5 | try enqueue | 1 | 1 | rear cannot increment → free space at index 0 is wasted |
Result: even though a free slot exists, it cannot be used because rear has reached the end of the array.
Comparison
| Aspect | Stack (linear array) | Queue (linear array) |
|---|---|---|
| Insert / delete end | Same end (top) | Opposite ends (rear for insert, front for delete) |
| Free space usage | Any remaining space is always utilized | Free space at the front becomes unusable once rear reaches the end |
| Practical use | Works fine | Rarely used due to wasted space |
Revision summary
- In a linear array queue, rear moves forward on enqueue and front moves forward on dequeue — both in the same direction.
- Once rear reaches the last index, the queue is treated as full even if free slots exist at the front.
- Deleted space at the front cannot be reused by rear → wasted memory.
- This drawback is why the linear array implementation is rarely used; the fix is to treat the array as circular.
Previous-year exam insights
- In a linear array queue with front and rear indices, the number of elements currently stored is rear − front + 1 (when non-empty);
the queue is empty when front > rear (or front = rear + 1) and full when rear = size − 1.
Asked inGATE 2013 - Amortized analysis: a sequence of n queue operations (enqueue/dequeue, including multi-dequeue loops) on an initially empty queue costs amortized per operation, so the total worst-case time is — each element is enqueued once and dequeued at most once.
Asked inGATE 2013 - A circular queue reuses freed front space by advancing indices modulo the array size: rear = (rear + 1) mod size and front = (front + 1) mod size;
the queue is full when (rear + 1) mod size = front (one slot sacrificed) and empty when front = rear. - Enqueue and dequeue on a queue (array- or list-based) are both operations; a queue is a FIFO structure, unlike a stack which is LIFO.
Asked inGATE 2013 - In a linear (non-circular) array queue, the condition rear = size − 1 signals 'full' regardless of how many slots at the front are free;
the number of wasted slots equals front (the number of dequeues performed so far). - A linked-list implementation of a queue avoids the wasted-space problem entirely, since nodes are allocated and freed dynamically; it needs both head (front) and tail (rear) pointers for enqueue and dequeue.