- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The Priority Queue in Javascript
In this article, we are going to discuss the priority queue data structure in JavaScript.
A priority queue is an abstract data type (ADT) which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.
Example 1
The following example demonstrates the priority queue class data structure in JavaScript.
Here, we insert the elements into the queue according to their priority. After creating a non-empty queue we can remove the element with the highest priority (by returning the front element using the shift() method).
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>PriorityQueue Data Structure</title> </head> <body> <script type="text/javascript"> class QueueElement { constructor(elem, priNo) { this.element = elem; this.priority = priNo; } } class PriorityQueue { constructor() { this.queArr = []; } enqueue(elem, priNo) { let queueElem = new QueueElement(elem, priNo); let contain = false; for (let i = 0; i < this.queArr.length; i++) { if (this.queArr[i].priority > queueElem.priority) { this.queArr.splice(i, 0, queueElem); contain = true; break; } } if (!contain) { this.queArr.push(queueElem); } } dequeue() { document.write( "</br>The dequeued element in the priority queue is : " ); if (this.isEmpty()) return "Underflow"; return this.queArr.shift(); } front() { if (this.isEmpty()) return "No elements in Queue"; return this.queArr[0]; } rear() { document.write("</br>The rear element of the priority queue is : "); if (this.isEmpty()) return "The Queue is Empty..!"; return this.queArr[this.queArr.length - 1]; } isEmpty() { return this.queArr.length == 0; } display() { document.write("The Elements in the priority queue are : </br>"); let res_Str = ""; for (let i = 0; i < this.queArr.length; i++) res_Str += this.queArr[i].element + " "; return res_Str; } } var priorityQueue = new PriorityQueue(); priorityQueue.enqueue("Alice", 2); priorityQueue.enqueue("Cullen", 4); priorityQueue.enqueue("Edward", 1); priorityQueue.enqueue("Bella", 2); priorityQueue.enqueue("Jacob", 3); document.write(priorityQueue.display()); </script> </body> </html>
Example 2
The following example demonstrates the implementation of priority queue class data structure in JavaScript.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>PriorityQueue Data Structure</title> </head> <body> <script type="text/javascript"> class QueueElement { constructor(elem, priNo) { this.element = elem; this.priority = priNo; } } class PriorityQueue { constructor() { this.queArr = []; } enqueue(elem, priNo) { let queueElem = new QueueElement(elem, priNo); let contain = false; for (let i = 0; i < this.queArr.length; i++) { if (this.queArr[i].priority > queueElem.priority) { this.queArr.splice(i, 0, queueElem); contain = true; break; } } if (!contain) { this.queArr.push(queueElem); } } dequeue() { document.write( "</br>The dequeued element in the priority queue is : " ); if (this.isEmpty()) return "Underflow"; return this.queArr.shift(); } front() { document.write("</br>The front element of the priority queue is : "); if (this.isEmpty()) return "The Queue is Empty..!"; return this.queArr[0]; } rear() { document.write("</br>The rear element of the priority queue is : "); if (this.isEmpty()) return "The Queue is Empty..!"; return this.queArr[this.queArr.length - 1]; } isEmpty() { return this.queArr.length == 0; } display() { document.write("The Elements in the priority queue are : </br>"); let res_Str = ""; for (let i = 0; i < this.queArr.length; i++) res_Str += this.queArr[i].element + " "; return res_Str; } } var priorityQueue = new PriorityQueue(); priorityQueue.enqueue("Alice", 2); priorityQueue.enqueue("Cullen", 4); priorityQueue.enqueue("Edward", 1); priorityQueue.enqueue("Bella", 2); priorityQueue.enqueue("Jacob", 3); document.write(priorityQueue.display()); document.write(priorityQueue.front().element); document.write(priorityQueue.rear().element); </script> </body> </html>
- Related Articles
- Creating a Priority Queue using Javascript
- Multithreaded Priority Queue in Python
- Meldable Priority Queue Operations
- Priority Queue Introduction in C/C++
- Dequeue and Priority Queue in C++
- Double Ended Priority Queue (DEPQ)
- Priority Queue using Linked List in C
- Double ended priority queue in C++ Program
- How to Implement Priority Queue in Python?
- C++ Program to Implement Priority Queue
- Priority Queue using doubly linked list in C++
- Priority Queue in C++ Standard Template Library (STL)
- STL Priority Queue for Structure or Class in C++
- Priority queue of pairs in C++ (Ordered by first)
- How to create Java Priority Queue to ignore duplicates?
