Queue
Overview
A Queue is an abstract data type (ADT) that follows the First In, First Out (FIFO) principle. The element that is added first is the first one to be removed.
Queues model orderly processing and fair scheduling.
What Is a Queue?
A queue:
- Allows insertion at the rear
- Allows removal from the front
- Maintains arrival order
- Restricts random access
Core Queue Operations
| Operation | Description |
|---|---|
| Enqueue | Add an element to the rear |
| Dequeue | Remove an element from the front |
| Front / Peek | View the front element |
| IsEmpty | Check if queue is empty |
| Size | Number of elements |
How It Works
Queues are commonly implemented using:
- Arrays (circular buffer)
- Linked lists
- Deques
The ADT defines behavior, not the internal structure.
Example
Pseudocode
queue.enqueue(10)
queue.enqueue(20)
queue.dequeue() // 10
queue.front() // 20
Real-world Analogy
A queue is like a line at a ticket counter. Whoever arrives first gets served first 🎟️🚶♀️🚶♂️.
Time Complexity
| Operation | Time |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Peek | O(1) |
Use Cases
- Task scheduling
- Print spooling
- Breadth-first search
- Producer–consumer systems
- Message queues
Advantages
- Fair processing order
- Simple abstraction
- Efficient operations
Limitations
- No random access
- Limited flexibility
- Fixed access pattern
Variants
- Circular queue
- Priority queue
- Double-ended queue (Deque)