Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1: Detailed Component - Queues

Lesson 4 of 14 in the free Data Structure notes on Siksha Sarovar, written by Rohit Jangra.

Queues: The FIFO Structure

Definition: A Queue is a linear data structure that permits insertion at one end (Rear) and deletion at the other end (Front). Principle: FIFO (First In, First Out). Think of a line at a ticket counter; the first person in line is the first served.

Queue Operations & Complexity

OperationDescriptionPointer ActionTime
EnqueueAdd element at RearRear = Rear + 1O(1)
DequeueRemove element at FrontFront = Front + 1O(1)
Front/PeekView front elementO(1)
isEmptyCheck if queue is emptyFront > RearO(1)
isFullCheck if queue is fullRear == MAX - 1O(1)

Limitations of Linear Queue — The "False Full" Problem

After multiple Enqueue/Dequeue operations, Rear reaches the end of the array, but empty spaces exist at the beginning. The queue appears full when it's not!

Example:

Initial:   [10, 20, 30, _, _]  Front=0, Rear=2
Dequeue 2: [_, _, 30, _, _]    Front=2, Rear=2
Enqueue:   [_, _, 30, 40, 50]  Front=2, Rear=4 (FULL!)

Positions 0 and 1 are wasted! Solution: Circular Queue.

Circular Queue — The Fix

Key Formula: Next Position = (Current + 1) % MAX_SIZE

This wraps the Rear pointer back to position 0 after reaching the end.

Worked Example (Size = 5):

OperationFrontRearQueue State
Enqueue(10)00[10, _, _, _, _]
Enqueue(20)01[10, 20, _, _, _]
Enqueue(30)02[10, 20, 30, _, _]
Dequeue() → 1012[_, 20, 30, _, _]
Dequeue() → 2022[_, _, 30, _, _]
Enqueue(40)23[_, _, 30, 40, _]
Enqueue(50)24[_, _, 30, 40, 50]
Enqueue(60)20[60, _, 30, 40, 50] ← Wraps!

Full Condition: (Rear + 1) % SIZE == Front Empty Condition: Front == -1 (or Front == Rear + 1 after dequeue)

Queue Variations — Comparison

VariationInsertDeleteOrderingUse Case
Linear QueueRear onlyFront onlyFIFOBasic task scheduling
Circular QueueRear (wraps)Front (wraps)FIFOMemory-efficient buffering
Deque (Double-Ended)Both endsBoth endsFlexibleSliding window, undo/redo
Priority QueueBy priorityHighest priority firstPriorityDijkstra's, CPU scheduling

Deque (Double-Ended Queue)

Definition: Insertion and deletion can be performed at both ends.

TypeInsertDelete
Input RestrictedONE end onlyBoth ends
Output RestrictedBoth endsONE end only

Priority Queue

Definition: Every element has an associated priority value. Elements are dequeued based on priority, not arrival time.

TypeRuleExample
Ascending (Min-Priority)Smallest value = Highest priorityEmergency room triage (severity 1 first)
Descending (Max-Priority)Largest value = Highest priorityVIP queue (highest rank first)

Tie-Breaking: If two items have the same priority, FIFO order is used.

Real-World Applications of Queues

ApplicationQueue Type Used
CPU SchedulingPriority Queue (OS processes)
Print SpoolerLinear Queue (first document prints first)
BFS Graph TraversalLinear Queue
Keyboard BufferCircular Queue (fixed-size input buffer)
Dijkstra's AlgorithmMin-Priority Queue
Streaming BufferCircular Queue (audio/video buffering)
Call CenterPriority Queue (VIP customers first)