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
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP