Module 2: Queues (The Waiting Line)
📚 Module 2: Queues
Course ID: DSA-106
Subject: The Waiting Line
A Queue is the opposite of a Stack. It follows the FIFO rule: First-In, First-Out.
🏗️ Step 1: FIFO Logic
🧩 The Analogy: The Coffee Shop Line
- The first person to arrive at the shop is the First person to get their coffee.
- New people join at the Back of the line.
- The person at the very Front is processed first.
🏗️ Step 2: The Two Operations
- ENQUEUE: Adding an item to the back of the line.
- DEQUEUE: Removing an item from the front of the line.
🏗️ Step 3: Why do we use it?
- Task Scheduling: Your computer CPU handles tasks in a queue. If you start 5 programs, they wait in a queue to be processed.
- Breadth-First Search (BFS): In Milestone 7, you’ll see that we use a Queue to explore maps and networks level by level.
🏗️ Step 4: The “Array Queue” Problem
If you use a normal Array to build a queue, it is very slow to remove from the front (because everyone has to shift forward).
- Senior Solution: We use a Circular Buffer or a Linked List to build a queue so that adding and removing are both O(1).
🥅 Module 2 Review
- FIFO: First-In, First-Out.
- Enqueue/Dequeue: Interaction from both ends.
- Efficiency: Seniors use specific structures to keep queues fast.
:::tip Slow Learner Note In Python, don’t use a standard list for a queue (it’s slow). Use collections.deque, which is a “Double-Ended Queue” built for speed! ::: Riverside. Riverside.