A queue is a linear data structure that follows the FIFO (First In, First Out) principle, which means the element inserted first is removed first. Queues are used in computer science to manage data in an organized manner. Depending on how elements are inserted and removed, queues are classified into different types, such as Simple Queue, Circular Queue, Priority Queue, and Double-Ended Queue (Deque). Understanding these types helps beginners learn how queues work and where they are used in real-world applications.
Table of Contents
What Are the Types of Queues?
A queue is a data structure in which elements are inserted and removed according to specific rules. The basic queue follows FIFO, but different queue types provide additional ways to manage elements.
The main types of queues are:
- Simple Queue: Insertion takes place at the rear, and deletion takes place at the front.
- Circular Queue: The last position connects logically to the first position to reuse available space.
- Priority Queue: Elements are processed according to their priority.
- Double-Ended Queue (Deque): Elements can be inserted and removed from both ends.
Each type is designed to solve different data management problems.
Simple Queue
A simple queue is the most basic type of queue. It follows the FIFO principle, where the first element inserted is the first element removed.
In a simple queue:
- Insertion is performed at the rear.
- Deletion is performed at the front.
- Elements are processed in their insertion order.
JavaScript Example:
// This program demonstrates a simple queue.
let queue = [];
function enqueue(element) {
queue.push(element);
}
function dequeue() {
if (queue.length === 0) {
return "Queue is empty";
}
return queue.shift();
}
enqueue(10);
enqueue(20);
enqueue(30);
console.log("Queue:", queue);
console.log("Removed Element:", dequeue());
console.log("Updated Queue:", queue);
Output:
Queue: [10, 20, 30]
Removed Element: 10
Updated Queue: [20, 30]
In this example, three elements are inserted into the queue. The dequeue operation removes 10 because it was inserted first.
Applications of Simple Queue:
- Printer job scheduling.
- Customer service systems.
- Task processing.
- Request handling.
Circular Queue
A circular queue is a type of queue in which the last position is logically connected to the first position. It allows the program to reuse empty spaces created after removing elements.
In a simple linear array-based queue, unused positions at the beginning may remain unavailable for new insertions. A circular queue solves this problem by using available positions again.
Key features:
- The last position can be followed by the first position.
- It uses available space efficiently.
- It follows FIFO order.
- It is commonly implemented using an array and front and rear indexes.
JavaScript Example:
// This program demonstrates a circular queue.
class CircularQueue {
constructor(capacity) {
this.capacity = capacity;
this.queue = new Array(capacity);
this.front = -1;
this.rear = -1;
}
enqueue(element) {
if ((this.rear + 1) % this.capacity === this.front) {
console.log("Queue is full");
return;
}
if (this.front === -1) {
this.front = 0;
}
this.rear = (this.rear + 1) % this.capacity;
this.queue[this.rear] = element;
}
dequeue() {
if (this.front === -1) {
console.log("Queue is empty");
return;
}
let removed = this.queue[this.front];
if (this.front === this.rear) {
this.front = -1;
this.rear = -1;
} else {
this.front = (this.front + 1) % this.capacity;
}
return removed;
}
display() {
let elements = [];
if (this.front === -1) {
console.log(elements);
return;
}
let i = this.front;
while (true) {
elements.push(this.queue[i]);
if (i === this.rear) {
break;
}
i = (i + 1) % this.capacity;
}
console.log(elements);
}
}
let queue = new CircularQueue(3);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
console.log("Removed:", queue.dequeue());
queue.enqueue(40);
queue.display();
Output:
Removed: 10
[20, 30, 40]
In this example, the queue has a capacity of three elements. After removing 10, the available position is reused to insert 40. This demonstrates how a circular queue uses its available space efficiently.
Applications of Circular Queue:
- CPU scheduling.
- Memory management.
- Keyboard buffers.
- Data streaming systems.
Priority Queue
A priority queue is a special type of queue in which elements are processed according to their priority rather than only their insertion order.
Each element is assigned a priority. The element with the highest priority is removed first. If two elements have the same priority, their insertion order can be maintained.
Priority queue implementations may use arrays, linked lists, or heaps.
Key features:
- Each element has an associated priority.
- Elements are processed according to priority.
- Higher-priority elements are selected before lower-priority elements.
- FIFO order may be used when priorities are equal.
JavaScript Example:
// This program demonstrates a priority queue.
class PriorityQueue {
constructor() {
this.items = [];
}
enqueue(element, priority) {
this.items.push({ element, priority });
this.items.sort((a, b) => a.priority - b.priority);
}
dequeue() {
if (this.items.length === 0) {
return "Queue is empty";
}
return this.items.shift().element;
}
display() {
console.log(this.items);
}
}
let queue = new PriorityQueue();
queue.enqueue("Normal Task", 3);
queue.enqueue("Urgent Task", 1);
queue.enqueue("Medium Task", 2);
console.log("Processed:", queue.dequeue());
queue.display();
Output:
Processed: Urgent Task
[{ element: "Medium Task", priority: 2 }, { element: "Normal Task", priority: 3 }]
In this example, each task is assigned a priority. A smaller priority number represents a higher priority. Therefore, the urgent task is removed first.
Applications of Priority Queue:
- Hospital emergency systems.
- Operating system scheduling.
- Network routing.
- Shortest-path algorithms such as Dijkstra's algorithm.
Double-Ended Queue (Deque)
A double-ended queue, commonly called a deque, is a special type of queue in which insertion and deletion can be performed at both ends.
Unlike a simple queue, a deque does not restrict insertion to the rear or deletion to the front.
Key features:
- Elements can be inserted at the front and rear.
- Elements can be removed from the front and rear.
- It provides more flexibility than a simple queue.
- It can be used to implement both stack and queue behavior.
JavaScript Example:
// This program demonstrates a double-ended queue.
let deque = [];
function insertFront(element) {
deque.unshift(element);
}
function insertRear(element) {
deque.push(element);
}
function deleteFront() {
return deque.shift();
}
function deleteRear() {
return deque.pop();
}
insertFront(10);
insertRear(20);
insertRear(30);
console.log("Deque:", deque);
deleteFront();
console.log("After Front Deletion:", deque);
deleteRear();
console.log("After Rear Deletion:", deque);
Output:
Deque: [10, 20, 30]
After Front Deletion: [20, 30]
After Rear Deletion: [20]
In this example, elements are inserted and removed from both ends of the deque. JavaScript array methods demonstrate the operations, although unshift() and shift() may take O(n) time.
Applications of Deque:
- Undo and redo operations.
- Sliding window algorithms.
- Palindrome checking.
- Browser history management.
Input-Restricted Deque
An input-restricted deque is a type of double-ended queue in which insertion is allowed at only one end, while deletion is allowed at both ends.
It provides more flexibility than a simple queue because elements can be removed from either end.
Key features:
- Insertion is allowed only at the rear.
- Deletion is allowed at both the front and rear.
- It follows specific insertion restrictions.
- It is useful when data must be added in one direction but removed from either end.
JavaScript Example:
// This program demonstrates an input-restricted deque.
let deque = [];
function insert(element) {
deque.push(element);
}
function deleteFront() {
return deque.shift();
}
function deleteRear() {
return deque.pop();
}
insert(10);
insert(20);
insert(30);
console.log("Deque:", deque);
console.log("Deleted from Front:", deleteFront());
console.log("Deleted from Rear:", deleteRear());
console.log("Updated Deque:", deque);
Output:
Deque: [10, 20, 30]
Deleted from Front: 10
Deleted from Rear: 30
Updated Deque: [20]
In this example, elements are inserted only at the rear, but deletion can take place from either end.
Output-Restricted Deque
An output-restricted deque is a type of double-ended queue in which insertion is allowed at both ends, while deletion is allowed at only one end.
It provides flexibility during insertion but restricts the removal operation.
Key features:
- Insertion is allowed at both the front and rear.
- Deletion is allowed only at the front.
- It follows specific deletion restrictions.
- It is useful when elements may arrive from either end but must be processed from one end.
JavaScript Example:
// This program demonstrates an output-restricted deque.
let deque = [];
function insertFront(element) {
deque.unshift(element);
}
function insertRear(element) {
deque.push(element);
}
function deleteFront() {
return deque.shift();
}
insertRear(10);
insertRear(20);
insertFront(5);
console.log("Deque:", deque);
console.log("Deleted Element:", deleteFront());
console.log("Updated Deque:", deque);
Output:
Deque: [5, 10, 20]
Deleted Element: 5
Updated Deque: [10, 20]
In this example, elements are inserted at both ends, but the deletion operation is performed only at the front.
Difference Between Types of Queues
The following table highlights the major differences between different types of queues.
| Queue Type | Insertion | Deletion | Main Feature |
|---|---|---|---|
| Simple Queue | Rear | Front | Follows FIFO |
| Circular Queue | Rear | Front | Reuses available space |
| Priority Queue | Based on implementation | Based on priority | Processes by priority |
| Deque | Both ends | Both ends | Flexible insertion and deletion |
| Input-Restricted Deque | One end | Both ends | Restricted insertion |
| Output-Restricted Deque | Both ends | One end | Restricted deletion |
Applications of Different Queues
Different types of queues are used in various computer science applications.
- Simple Queue: Used in printer scheduling, customer service systems, and task management.
- Circular Queue: Used in CPU scheduling, memory buffers, and streaming applications.
- Priority Queue: Used in emergency services, operating systems, and graph algorithms.
- Deque: Used in sliding window problems, palindrome checking, and undo operations.
- Input-Restricted Deque: Used in systems where insertion must happen at one end but removal can happen at either end.
- Output-Restricted Deque: Used in systems where insertion can happen at either end but removal must happen at one end.
Common Mistakes to Avoid
- Confusing a simple queue with a circular queue: A circular queue reuses available positions, while a basic linear queue does not automatically wrap around.
- Assuming a priority queue always follows FIFO: Priority queues process elements according to priority, not necessarily insertion order.
- Confusing deque restrictions: An input-restricted deque limits insertion, while an output-restricted deque limits deletion.
- Ignoring empty and full conditions: Always check whether a fixed-capacity queue is empty or full before performing an operation.
- Using the wrong insertion or deletion end: Understand the rules of each queue type before implementing its operations.
Frequently Asked Questions
1. What are the main types of queues in data structures?
The main types are Simple Queue, Circular Queue, Priority Queue, and Double-Ended Queue (Deque).
2. Which queue follows the FIFO principle?
A simple queue follows the FIFO principle, where the first inserted element is the first one removed.
3. What is the main advantage of a circular queue?
A circular queue reuses available positions and helps avoid wasting space in a fixed-size array.
4. What is the difference between a queue and a deque?
A queue allows insertion at the rear and deletion at the front, while a deque allows insertion and deletion at both ends.
5. What is the difference between an input-restricted and an output-restricted deque?
An input-restricted deque allows insertion at only one end and deletion at both ends. An output-restricted deque allows insertion at both ends but deletion at only one end.
0 Comments