A simple implementation of the Priority Queue data structure in JavaScript. This repository demonstrates how to create a priority queue class with essential methods and explains its functionality with practical examples.
A Priority Queue is a data structure that stores elements in a way that allows efficient access to the element with the highest priority. Each element in the queue is assigned a priority value, and elements with higher priority are dequeued before elements with lower priority. Priority queues are commonly implemented using heaps.
- Enqueue: Add an element to the queue with a given priority.
- Dequeue: Remove and return the element with the highest priority.
- Peek: View the element with the highest priority without removing it.
- isEmpty: Check if the queue is empty.
- Size: Get the number of elements in the queue.
Here’s the JavaScript implementation of the priority queue:
class PriorityQueue {
constructor() {
this.queue = [];
}
// Add an element to the queue with a given priority
enqueue(element, priority) {
const item = { element, priority };
if (this.isEmpty()) {
this.queue.push(item);
} else {
let added = false;
for (let i = 0; i < this.queue.length; i++) {
if (item.priority > this.queue[i].priority) {
this.queue.splice(i, 0, item);
added = true;
break;
}
}
if (!added) {
this.queue.push(item);
}
}
}
// Remove and return the element with the highest priority
dequeue() {
if (this.isEmpty()) {
return "Queue is empty!";
}
return this.queue.shift();
}
// View the element with the highest priority without removing it
peek() {
if (this.isEmpty()) {
return "Queue is empty!";
}
return this.queue[0];
}
// Check if the queue is empty
isEmpty() {
return this.queue.length === 0;
}
// Get the size of the queue
size() {
return this.queue.length;
}
}
// Initialize the priority queue
const pq = new PriorityQueue();
// Enqueue elements with priorities
pq.enqueue("Task 1", 1);
pq.enqueue("Task 2", 3);
pq.enqueue("Task 3", 2);
// Peek at the highest priority element
console.log(pq.peek()); // Output: { element: 'Task 2', priority: 3 }
// Dequeue elements
console.log(pq.dequeue()); // Output: { element: 'Task 2', priority: 3 }
console.log(pq.dequeue()); // Output: { element: 'Task 3', priority: 2 }
// Check if the queue is empty
console.log(pq.isEmpty()); // Output: false
// Get the size of the queue
console.log(pq.size()); // Output: 1
- Task Scheduling: Managing tasks based on priority in operating systems.
- Dijkstra's Algorithm: Finding the shortest path in graphs.
- Job Scheduling: For scheduling jobs with different priorities in computers.
- Bandwidth Management: Prioritizing network traffic based on importance.
Want to see a quick tutorial on how to build this? Check out this TikTok video:
- Clone the repository:
git clone https://github.com/fix2015/structure_priority_queue cd structure_priority_queue
- Open the file
index.js
in your favorite code editor. - Run the file using Node.js:
node index.js
Contributions are welcome! If you have suggestions or want to add new features, feel free to create a pull request.
This project is licensed under the MIT License.
- LinkedIn - Vitalii Semianchuk
- Telegram - @jsmentorfree - We do a lot of free teaching on this channel! Join us to learn and grow in web development.
- Tiktok - @jsmentoring Everyday new videos
- Youtube - @jsmentor-uk Mentor live streams
- Dev.to - fix2015 Javascript featured, live, experience but about Priority Queue