Types of Queues: Simple, Circular, and Priority Queues | Data Structures and Algorithms Day #9
Explore the different types of queues: simple, circular, and priority

Software Engineer & Technical Writer
Search for a command to run...
Explore the different types of queues: simple, circular, and priority

Software Engineer & Technical Writer
No comments yet. Be the first to comment.
In this series, I will walk you through Data Structures and Algorithms and help you prepare for coding interviews.
Learn about the queue data structure and its operations with Python and JavaScript examples
affordable coding bootcamps in Kenya

Top 12 Data Structure Interview Questions Every Developer Should Know

If you're on a journey to becoming a skilled web developer or full-stack engineer, finding the right resources is essential. GitHub is a goldmine for such resources, with repositories that cover everything from basic HTML and CSS to advanced topics l...

Whether you're starting out in software engineering or looking to refine your skills, GitHub is a treasure trove of high-quality resources. Here’s a list of 9 GitHub repositories that can help you grow your technical knowledge, deepen your problem-so...

Python has a vast ecosystem of command-line tools, but installing these globally with pip can lead to version conflicts and dependency issues. pipx offers a modern solution by enabling global installations in isolated environments. In this guide, we'...

Understanding the different types of queues is crucial for mastering data structures. Queues operate based on the FIFO (First-In-First-Out) principle, making them essential in situations where elements need to be processed sequentially, such as task scheduling, handling requests, or managing buffers. In this article, we’ll explore the simple queue, circular queue, and priority queue, discuss their operations, and look at Python and JavaScript code examples for each type.
A simple queue, also called a linear queue, is the most basic type of queue. It follows the FIFO principle, meaning the first element added is the first to be removed.
Enqueue – Add an element to the rear of the queue.
Dequeue – Remove an element from the front.
Peek – View the front element without removing it.
isEmpty – Check if the queue is empty.
class SimpleQueue:
def __init__(self):
self.queue = []
def enqueue(self, item):
self.queue.append(item)
def dequeue(self):
if not self.is_empty():
return self.queue.pop(0)
return "Queue is empty!"
def peek(self):
return self.queue[0] if not self.is_empty() else None
def is_empty(self):
return len(self.queue) == 0
# Example Usage
q = SimpleQueue()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue()) # Output: 1
class SimpleQueue {
constructor() {
this.queue = [];
}
enqueue(item) {
this.queue.push(item);
}
dequeue() {
return this.queue.length ? this.queue.shift() : "Queue is empty!";
}
peek() {
return this.queue[0] || null;
}
isEmpty() {
return this.queue.length === 0;
}
}
// Example Usage
const q = new SimpleQueue();
q.enqueue(1);
q.enqueue(2);
console.log(q.dequeue()); // Output: 1
A circular queue connects the rear and front of the queue to create a loop, allowing efficient use of space. When the end of the queue is reached, new elements can be added at the beginning if there is space available.

class CircularQueue:
def __init__(self, size):
self.queue = [None] * size
self.size = size
self.front = self.rear = -1
def enqueue(self, item):
if (self.rear + 1) % self.size == self.front:
return "Queue is full!"
elif self.front == -1:
self.front = self.rear = 0
else:
self.rear = (self.rear + 1) % self.size
self.queue[self.rear] = item
def dequeue(self):
if self.front == -1:
return "Queue is empty!"
item = self.queue[self.front]
if self.front == self.rear:
self.front = self.rear = -1 # Queue is now empty
else:
self.front = (self.front + 1) % self.size
return item
A priority queue allows elements to be dequeued based on their priority rather than their arrival order. This is useful in scenarios like scheduling or pathfinding algorithms.
class PriorityQueue {
constructor() {
this.queue = [];
}
enqueue(item, priority) {
const newItem = { item, priority };
let added = false;
for (let i = 0; i < this.queue.length; i++) {
if (this.queue[i].priority > priority) {
this.queue.splice(i, 0, newItem);
added = true;
break;
}
}
if (!added) this.queue.push(newItem);
}
dequeue() {
return this.queue.length ? this.queue.shift() : "Queue is empty!";
}
}
| Queue Type | Structure | Use Case |
| Simple Queue | Linear | General-purpose FIFO processing |
| Circular Queue | Circular, with wrapped ends | Efficient memory usage |
| Priority Queue | Based on priority, not FIFO | Task scheduling, pathfinding |
| Operation | Simple Queue | Circular Queue | Priority Queue |
| Enqueue | O(1) | O(1) | O(n) |
| Dequeue | O(1) | O(1) | O(1) |
| Peek | O(1) | O(1) | O(1) |
Simple Queue: Printer job scheduling, task management.
Circular Queue: Memory management in embedded systems.
Priority Queue: Task scheduling in operating systems and Dijkstra’s algorithm.
A circular queue wraps around when the end is reached, utilizing empty spaces more efficiently.
A priority queue processes elements based on their priority rather than the order of insertion.
They are used in task scheduling, network routing, and shortest path algorithms.
Try implementing these queue types in Python or JavaScript using the code examples provided, and explore their applications further in your projects.