Queue in Data Structure — Types & Algorithms (with Examples)
A Queue is a linear data structure that follows the FIFO (First In, First Out) principle: the first element added is the first one removed.
Core Operations
enqueue(item)— insert an element at the rear.dequeue()— remove and return the element at the front.peek()— view the front element without removing it.isEmpty()— check whether the queue has any elements.
Types of Queues
| Type | Description |
|---|---|
| Simple Queue | Basic FIFO queue, insert at rear, remove from front. |
| Circular Queue | Rear wraps back to the front to reuse freed space. |
| Priority Queue | Elements are served according to priority, not order of arrival. |
| Deque | Insertion and removal allowed at both ends. |
from collections import deque
class Queue:
def __init__(self):
self.items = deque()
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
return self.items.popleft()
def peek(self):
return self.items[0]
def is_empty(self):
return len(self.items) == 0
Complexity
| Operation | Time |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Peek | O(1) |
Applications
- Task and process scheduling in operating systems.
- Breadth First Search traversal in graphs.
- Handling of asynchronous data such as I/O buffers and print queues.
Ready to master Data Structures & Algorithms?
Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.
.png)