DSA Queues

DSA Tutorial

 DSA Queues – Complete Beginner Guide

(Concept • Types • Operations • Implementation • Examples • Complexity)

A Queue is a fundamental linear data structure in DSA that follows the FIFO principle.

FIFO = First In, First Out


1️⃣ What is a Queue?

A Queue is a data structure where:

  • Insertion happens at the rear

  • Deletion happens from the front

Real-Life Examples

  • People standing in a line

  • Ticket booking system

  • CPU task scheduling

  • Printer queue


2️⃣ Queue Terminology

TermMeaning
FrontFirst element
RearLast element
EnqueueInsert element
DequeueRemove element
OverflowQueue full
UnderflowQueue empty

3️⃣ Basic Queue Operations

  1. Enqueue – Insert element at rear

  2. Dequeue – Remove element from front

  3. Peek / Front – View front element

  4. isEmpty – Check if empty

  5. isFull – Check if full


4️⃣ Simple Queue (Array Implementation)

 Code (C++)


 


5️⃣ Limitations of Simple Queue ❌

❌ Wastage of space after dequeuing
❌ Rear reaches end even if front has space

📌 Solution → Circular Queue


6️⃣ Circular Queue

 Concept

  • Rear connects back to front

  • Uses modulo operation %

 Code (C++)


 


7️⃣ Queue Using Linked List


 


8️⃣ Types of Queues

TypeDescription
Simple QueueLinear FIFO
Circular QueueLast connects to first
Priority QueueHigher priority served first
DequeInsert/delete both ends

9️⃣ Deque (Double Ended Queue)

  • Insert & delete from both ends

  • Two types:

    • Input Restricted

    • Output Restricted

📌 Used in sliding window problems.


🔟 Time Complexity of Queue Operations

OperationTime
EnqueueO(1)
DequeueO(1)
PeekO(1)
SearchO(n)

1️⃣1️⃣ Queue Applications

✔ CPU scheduling
✔ BFS (Breadth First Search)
✔ Printer scheduling
✔ Network buffering
✔ Producer–Consumer problem


Interview Questions (Queue)

Q1. What is FIFO?
👉 First In First Out

Q2. Why circular queue is better than simple queue?
👉 Prevents memory wastage

Q3. Difference between queue & stack?
👉 FIFO vs LIFO


Final Summary

✔ Queue follows FIFO
✔ Insert at rear, delete from front
✔ Circular queue improves efficiency
✔ Linked list queue is dynamic
✔ Very important for DSA interviews

You may also like...