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:
classPriorityQueue{constructor(){this.queue=[];}// Add an element to the queue with a given priorityenqueue(element,priority){constitem={ element, priority };if(this.isEmpty()){this.queue.push(item);}else{letadded=false;for(leti=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 prioritydequeue(){if(this.isEmpty()){return"Queue is empty!";}returnthis.queue.shift();}// View the element with the highest priority without removing itpeek(){if(this.isEmpty()){return"Queue is empty!";}returnthis.queue[0];}// Check if the queue is emptyisEmpty(){returnthis.queue.length===0;}// Get the size of the queuesize(){returnthis.queue.length;}}// Initialize the priority queueconstpq=newPriorityQueue();// Enqueue elements with prioritiespq.enqueue("Task 1",1);pq.enqueue("Task 2",3);pq.enqueue("Task 3",2);// Peek at the highest priority elementconsole.log(pq.peek());// Output: { element: 'Task 2', priority: 3 }// Dequeue elementsconsole.log(pq.dequeue());// Output: { element: 'Task 2', priority: 3 }console.log(pq.dequeue());// Output: { element: 'Task 3', priority: 2 }// Check if the queue is emptyconsole.log(pq.isEmpty());// Output: false// Get the size of the queueconsole.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.jsin 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