-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityQueue.js
43 lines (42 loc) · 1.08 KB
/
priorityQueue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 优先级队列,数字小的优先级高;
*
*/
class priorityQueue{
constructor(){
this.priority = [];
}
enqueue(value,_priority){
const queueELement = {value,_priority}
if(this.priority.length == 0){
this.priority.push(value)
}else {
const preIndex = this.priority.findIndex( (item)=> queueELement._priority < item._priority )
if(preIndex > -1 ){
this.priority.splice(preIndex,0,queueELement); // 相当于添加元素;
}else {
this.priority.push(queueELement);
}
}
}
dequeue(){
return this.priority.shift(); // 返回第一个元素;
}
front(){
return this.priority[0];
}
clear(){
this.priority = []
}
getSize(){
return this.priority.length;
}
isEmpty(){
return !this.priority.length;
}
show(){
console.log("展示队列:",this.priority)
return this.priority;
}
}
module.exports = priorityQueue