Repository navigation
Expand file tree
/
Copy pathQueue.js
More file actions
112 lines (94 loc) · 2.47 KB
/
Copy pathQueue.js
File metadata and controls
112 lines (94 loc) · 2.47 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var Queue = function () {
//使用数组作为队列的载体
var items = [];
//入列
this.enqueue = function (element) {
items.push(element);
}
//出列
this.dequeue = function () {
return items.shift();
}
//查看队列头
this.front = function () {
return items[0];
}
//检查队列是否为空
this.isEmpty = function () {
return items.length === 0;
}
//获取队列长度
this.size = function () {
return items.length;
}
//获取全部队列
this.getItems = function () {
return items;
}
}
//击鼓传花例题
var chuanhua = function (number) {
//
var q = new Queue;
var gamer = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
for (var i = 0; i < gamer.length; i++) {
q.enqueue(gamer[i])
}
while (q.size() > 1) {
var j = 1
while (j < number) {
q.enqueue(q.dequeue());
j++;
}
var out = q.dequeue();
console.log(out);
}
return q.front();
}
//优先队列
var PriorityQueue = function () {
var items = [];
//设定一个辅助类,存储队列元素的和队列元素的优先级数据
var QueueItem = function (element, priority) {
this.element = element;
this.priority = priority;
}
//入列
this.enqueue = function (element, priority) {
var queueItem = new QueueItem(element, priority);
//设定一个辅助变量,判断元素是否已经插入队列
var add = false;
//遍历队列检查每个元素的优先级
for (var i = 0; i < items.length; i++) {
//加入待插入元素的优先级大于遍历的元素,则在该元素前插入元素
if (queueItem.priority > items[i].priority) {
items.splice(i, 0, queueItem);
//插入后辅助变量变为true,退出循环
add = true;
break;
}
}
if (!add) {
items.push(queueItem);
}
}
//出列
this.dequeue = function () {
return items.shift();
}
//查看队列头
this.front = function () {
return items[0];
}
//检查队列是否为空
this.isEmpty = function () {
return items.length === 0;
}
//获取队列长度
this.size = function () {
return items.length;
}
this.getItems = function () {
return items;
}
}