Data Structures · Queues
Front and Rear Pointers for Array-Based Queue
Overview
A queue is a linear data structure in which insertion happens at one end and deletion at the other, giving First In, First Out (FIFO) behaviour.
When a queue is implemented with an array, two pointers — front and rear — are maintained to track the first and last inserted elements.
Key concepts
- Queue: a linear data structure, like a stack, but insertion and deletion occur at opposite ends.
- FIFO (First In, First Out): the first element inserted is the first to come out; the last inserted is the last to come out. A queue behaves like a real-life line (e.g., standing in line to book a ticket).
- Stack: a linear data structure that is LIFO (Last In, First Out) — the first element inserted is the last to come out, and the last inserted is the first to come out. Insertion and deletion both happen at the same end.
- front: points to the first element inserted — the element that will be deleted first.
- rear: points to the last element inserted — the most recently inserted element.
- Enqueue: insertion into a queue, also called add or insert. (Stack insertion is called push.)
- Why two pointers: a stack needs only one pointer (to the last inserted element, which is the one removed), but a queue must track both the first element (for deletion) and the last location (because additions are done at the end).

How it works
- Initially, front and rear point to nothing — the queue is empty.
- Insert the first element (say A): after insertion, both front and rear point to A.
- For each subsequent insertion: increment rear first, then insert the element at the new rear position.
- Repeat the increment-rear-then-insert step for every further element.
- As a result, rear always points to the latest element inserted and front always points to the first element inserted.
Comparison
| Feature | Stack | Queue |
|---|---|---|
| Insertion side | Same side as deletion | One side (rear) |
| Deletion side | Same side as insertion | Other side (front) |
| Order | LIFO (Last In, First Out) | FIFO (First In, First Out) |
| Insert operation name | push | add / insert / enqueue |
| Pointers needed (array implementation) | One pointer (top) | Two pointers (front and rear) |
| First element to come out | Last inserted | First inserted |
Worked examples
Array-based queue, initially front and rear point to nothing (empty).
| Step | Operation | Action on pointers | Queue contents (front → rear) |
|---|---|---|---|
| 1 | enqueue(A) | front = rear = position of A | A |
| 2 | enqueue(B) | increment rear, insert B at new rear | A, B |
| 3 | enqueue(C) | increment rear, insert C at new rear | A, B, C |
- front points to A (first inserted).
- rear points to C (last inserted).
Revision summary
- Queue = FIFO; Stack = LIFO. "First in, last out" is not the queue.
- Array-based queue uses two pointers: front and rear.
- Initially front and rear point to nothing (empty queue).
- After inserting the first element, both front and rear point to it.
- For every further insertion: increment rear first, then insert.
- front → first element inserted (next to be deleted); rear → last element inserted.
- Two pointers are required because additions happen at the end (rear) while the first element (front) must be tracked for deletion.
Previous-year exam insights
- In a non-circular array queue, each enqueue does rear = rear + 1 and each dequeue does front = front + 1;
the queue is empty when front > rear (front == rear + 1), and full when rear == MAX-1.
This causes the 'false overflow' / drifting problem: space freed at the front cannot be reused.
Asked inGATE 2004GATE 2013 - A circular queue removes false overflow: rear = (rear + 1) mod MAX and front = (front + 1) mod MAX;
empty when front == rear (with a count or one slot sacrificed) and full when (rear + 1) mod MAX == front.
Asked inGATE 2004 - A queue can be implemented with two stacks (enqueue onto one, dequeue from the other, transferring when the second is empty), so the minimum number of stacks of size n needed to implement a queue of size n is two.
Asked inGATE 2001 - With a singly linked list queue, keeping both head (front) and tail (rear) pointers gives enqueue at the tail and dequeue at the head; deleting at the tail requires because the predecessor must be found.
Asked inGATE 2018 - A queue can be represented by a circular singly linked list with a single pointer to the rear node: front is rear->next, so both enqueue (insert after rear) and dequeue (remove rear->next) take .
Asked inGATE 2004 - A deque (double-ended queue) supports insertFirst, insertLast, removeFirst and removeLast, each in in a suitable implementation; a queue is a deque restricted to insertLast and removeFirst.
Asked inGATE 2024