-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
156 lines (145 loc) · 3.43 KB
/
index.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo02</title>
</head>
<body>
<h1>demo02:队列的实现(普通队列,优先队列,循环队列)</h1>
<h4>请打开调试工具(F12)</h4>
</body>
</html>
<script>
// -------------------【1】默认队列------------------------------
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(){
return items.length == 0;
};
//队列的元素个数
this.size = function(){
return items.length;
};
//清空队列
this.clear = function(){
items = [];
};
//输出全队列
this.print = function(){
console.log(items.toString());
};
}
// ----------------使用默认队列---------------------------
// var queue = new Queue();
// console.log(queue.isEmpty()); // true
// queue.enqueue(5);
// console.log(queue.isEmpty()); //false
// queue.enqueue(8);
// queue.print(); //5,8
// queue.dequeue();
// queue.print(); //8
//-------------------【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);
for(var i=0; i < items.length;i++)
{
if( queueElement.priority > items[i].priority)
{
if(i == 0)
{
items.unshift(queueElement);
}
else
{
items.splice(i,0,queueElement);
}
break;
}
}
if(i == items.length)
{
items.push(queueElement);
}
};
//其余同普通队列一样
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.clear = function(){
items = [];
};
this.print = function(){
console.log(JSON.stringify(items));
};
}
//-------------------使用优先队列--------------------------
var priorityQueue = new PriorityQueue();
console.log(priorityQueue.isEmpty()); // true
priorityQueue.enqueue('wang',8);
priorityQueue.enqueue('li',7);
priorityQueue.enqueue('chen',9);
priorityQueue.enqueue('si',10);
priorityQueue.enqueue('qie',80);
priorityQueue.dequeue();
priorityQueue.print();
//[{"element":"si","priority":10},
//{"element":"chen","priority":9},
//{"element":"wang","priority":8},
//{"element":"li","priority":7}]
//------------------【3】循环队列------------------------------
//实现击鼓传花游戏,最后一个在场的为胜利者,num为击鼓次数
function hotPotato(nameList,num)
{
var queue = new Queue();
//参赛名字入队列
for(let i = 0; i < nameList.length; i++)
{
queue.enqueue(nameList[i]);
}
var outName = '';
while(queue.size > 1)
{
for(let i=0;i<num;i++)
{
//队列头变为队列为,形成循环队列
queue.enqueue(queue.dequeue());
}
outName = queue.dequeue();
console.log(outName+"被淘汰");
}
return queue.dequeue();
}
// var names = ['one','two','three','four','five'];
// console.log("胜利者是:"+hotPotato(names,7)); //假定击鼓7次
</script>