What is a Queue?
- A Queue is a linear data structure that follows the FIFO principle:
First In, First Out
- The first element added is the first one to be removed.
Real-Life Examples
- Waiting line at a ticket counter: First person in line gets served first.
- Printer queue: Documents are printed in the order they were added.
- Task scheduling: Tasks are processed in the order they arrive.
Basic Queue Operations
| Operation | Description |
|---|
enqueue() | Add an element to the rear of the queue |
dequeue() | Remove and return the front element |
peek() / front() | View the front element without removing it |
isEmpty() | Check if the queue is empty |
Queue Implementation
Queues can be implemented using:
- Arrays
- Linked Lists
- Two Stacks (for advanced scenarios)
Types of Queues
| Type | Description |
|---|
| Simple Queue | Basic FIFO structure |
| Circular Queue | Last position is connected back to the first |
| Deque (Double Ended Queue) | Insertion/deletion from both ends |
| Priority Queue | Elements are processed based on priority |
Time Complexity
| Operation | Time Complexity |
|---|
| enqueue | O(1) |
| dequeue | O(1) |
| peek | O(1) |
Like Stack, Queue operations are also highly efficient.
Applications of Queue
- CPU and disk scheduling
- Breadth-First Search (BFS) in graphs
- Data buffering (e.g., IO Buffers)
- Handling requests in web servers