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>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP