-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
142 lines (142 loc) · 4.19 KB
/
Copy pathqueue.js
File metadata and controls
142 lines (142 loc) · 4.19 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
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
/*
创建一个类来表示队列
*/
var Queue = /** @class */ (function () {
//初始化私有变量items数组,用于保存队列中的所有元素
function Queue() {
this.items = [];
}
//向队列中添加一个元素
Queue.prototype.enqueue = function (ele) {
this.items.push(ele);
};
//移除队列的第一项 并返回移除的项
Queue.prototype.dequeue = function () {
return this.items.shift();
};
//返回队列的第一个元素
Queue.prototype.front = function () {
return this.items[0];
};
//检查队列中是否还包含元素,为空则返回true
Queue.prototype.isEmpty = function () {
return this.items.length === 0;
};
//返回队列中元素的个数
Queue.prototype.size = function () {
return this.items.length;
};
//打印队列中的元素
Queue.prototype.print = function () {
console.log(this.items.toString());
};
return Queue;
}());
/*
到此我们的Queue类就实现,使用Queue类:
*/
var queue = new Queue();
console.log(queue.isEmpty()); //输出true
//添加一些元素
queue.enqueue('xiaoming');
queue.enqueue('xiaohong');
queue.enqueue('xiaogang');
//打印一下
queue.print(); // 'xiaoming','xiaohong','xiaogang';
console.log(queue.size()); // 输出3
queue.dequeue(); // 'xiaoming'
queue.print(); // 'xiaohong' , 'xiaogang'
var PriorityQueue = /** @class */ (function () {
//初始化私有变量items数组,用于保存队列中的所有元素,数组中存储对象
function PriorityQueue() {
this.items = [];
this.add = false;
}
//向队列中添加一个元素
PriorityQueue.prototype.enqueue = function (ele) {
for (var i = 0; i < this.items.length; i++) {
if (ele.prirority < this.items[i].prirority) {
this.items.splice(i, 0, ele);
this.add = true;
break;
}
}
if (!this.add) {
console.log(this.add);
this.items.push(ele);
}
};
//移除队列的第一项 并返回移除的项
PriorityQueue.prototype.dequeue = function () {
return this.items.shift();
};
//返回队列的第一个元素
PriorityQueue.prototype.front = function () {
return this.items[0];
};
//检查队列中是否还包含元素,为空则返回true
PriorityQueue.prototype.isEmpty = function () {
return this.items.length === 0;
};
//返回队列中元素的个数
PriorityQueue.prototype.size = function () {
return this.items.length;
};
//打印队列中的元素
PriorityQueue.prototype.print = function () {
console.log(this.items.length);
this.items.forEach(function (item) {
console.log(item.ele + " ----- " + item.prirority);
});
};
return PriorityQueue;
}());
var priorityQueue = new PriorityQueue();
priorityQueue.enqueue({ ele: '小红', prirority: 1 });
priorityQueue.enqueue({ ele: '小刚', prirority: 4 });
priorityQueue.print(); // 小红---1,小明---3,小刚---4
console.log(priorityQueue.size()); // 3
/*
循环队列-击鼓传花
*/
function hotPotato(nameList, num) {
var queue = new Queue();
var name;
console.log('123');
//将nameList存入 queue 中
nameList.forEach(function (item) {
queue.enqueue(item);
});
while (queue.size() > 1) {
for (var i = 0; i < num; i++) {
queue.enqueue(queue.dequeue());
}
name = queue.dequeue();
console.log(name + '被淘汰了');
}
name = queue.dequeue();
return name;
}
var nameList = ['小明', '小红', '小刚', '小强'];
hotPotato(nameList, 4); //小强
var num = [1, 3, 5, 7];
function ac(arr, num) {
var start = 0, end = arr.length - 1, mid = null, guess = null;
while (start <= end) {
mid = Math.floor((start + end) / 2);
guess = arr[mid];
if (guess < num) {
start = mid + 1;
}
else if (guess > num) {
end = mid - 1;
}
else if (guess === num) {
return mid;
}
else {
return null;
}
}
}
console.log(ac(num, 1));