JavaScript 中的优先级队列
在本文中,我们将讨论 JavaScript 中的优先队列数据结构。
优先队列是一种抽象数据类型(ADT),类似于常规队列或栈数据结构,但除此之外,每个元素都与一个“优先级”相关联。在优先队列中,高优先级的元素在低优先级的元素之前得到处理。如果两个元素具有相同的优先级,则按照它们在队列中的顺序进行处理。
示例 1
以下示例演示了 JavaScript 中的优先队列类数据结构。
在这里,我们根据元素的优先级将元素插入队列。在创建一个非空队列之后,我们可以移除具有最高优先级的元素(通过使用 shift() 方法返回前端元素)。
<!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>
示例 2
以下示例演示了 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>
广告