-
Notifications
You must be signed in to change notification settings - Fork 65
/
Queue.js
70 lines (64 loc) · 1.86 KB
/
Queue.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 队列
function Queue() {
var items = [];
this.enqueue = function (element) { //向队列尾部添加一个新的项
items.push(element);
}
this.dequeue = function() { // 移除队列的第一项,并返回被移除的元素
return items.shift();
}
this.front = function() { // 返回队列的第一个元素
return items[0]
}
this.isEmpty = function() { //检查队列中是否有元素,返回true或false
return items.length == 0;
}
this.size = function() {
return items.length;
}
this.print = function() {
console.log(items.toString());
}
}
// 使用Queue类
var queue = new Queue();
console.log(queue.isEmpty())
queue.enqueue('John');
queue.enqueue('Jack');
queue.enqueue('Camial');
queue.print();
queue.dequeue();
queue.print();
//优先队列
// 1.设置优先级,然后在正确的位置添加元素
// 2.用入列操作添加元素,然后按照优先级移除他们。
function priorityQueue() {
var items = [];
function QueueElement (element,priority) {
this.element = element;
this.priority = priority;
}
this.enqueue = function(element,priority){
var queueElement = new QueueElement(element,priority);
if(this.isEmpty()) {
items.push(queueElement);
}else {
var added = false;
for (var i= 0;i<items.length;i++){
if(queueElement.priority<items[i].priority) {
items.splice(i,0,queueElement);
added = true ;
break;
}
}
if(!added) {
items.push(queueElement);
}
}
}
}
var priorityQueue = new priorityQueue();
priorityQueue.enqueue('john',2);
priorityQueue.enqueue('jack',1);
priorityQueue.enqueue('camila',1);
priorityQueue.print();