1. Gyanastra
  2. Data Structures
  3. Queues
  4. Front and Rear Pointers for Array-Based Queue
Free lesson, no account needed 5 min read 14 practice questions after it
Asked in past papers16 times
  • GATE CSE20262024202320222021201820162013200720042001
  • UGC NET20172016201320122005

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).
Data structure diagram: Front and Rear Pointers for Array-Based Queue
Figure. Front and Rear Pointers for Array-Based Queue

How it works

  1. Initially, front and rear point to nothing — the queue is empty.
  2. Insert the first element (say A): after insertion, both front and rear point to A.
  3. For each subsequent insertion: increment rear first, then insert the element at the new rear position.
  4. Repeat the increment-rear-then-insert step for every further element.
  5. As a result, rear always points to the latest element inserted and front always points to the first element inserted.

Comparison

FeatureStackQueue
Insertion sideSame side as deletionOne side (rear)
Deletion sideSame side as insertionOther side (front)
OrderLIFO (Last In, First Out)FIFO (First In, First Out)
Insert operation namepushadd / insert / enqueue
Pointers needed (array implementation)One pointer (top)Two pointers (front and rear)
First element to come outLast insertedFirst inserted

Worked examples

Array-based queue, initially front and rear point to nothing (empty).

StepOperationAction on pointersQueue contents (front → rear)
1enqueue(A)front = rear = position of AA
2enqueue(B)increment rear, insert B at new rearA, B
3enqueue(C)increment rear, insert C at new rearA, 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 Θ(1)\Theta(1) enqueue at the tail and Θ(1)\Theta(1) dequeue at the head; deleting at the tail requires Θ(n)\Theta(n) 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 O(1)O(1).
    Asked inGATE 2004
  • A deque (double-ended queue) supports insertFirst, insertLast, removeFirst and removeLast, each in O(1)O(1) in a suitable implementation; a queue is a deque restricted to insertLast and removeFirst.
    Asked inGATE 2024
Pages 1–2 of 2