Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6176fd2

Browse files
feat: 队列
1 parent 686508c commit 6176fd2

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

‎栈和队列/创建循环队列.js‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Queue {
2+
constructor(n) {
3+
this.arr = new Array(n);
4+
this.size = 0; // 元素个数
5+
this.length = 0;
6+
this.front = 0;
7+
this.tail = 0;
8+
}
9+
}
10+
Queue.prototype.enqueue = function (val) {
11+
if (this.full()) return;
12+
this.arr[this.tail] = val;
13+
this.tail = ++this.tail % this.arr.length;
14+
// if (this.tail === this.arr.length) {
15+
// this.tail = 0;
16+
// }
17+
this.size++;
18+
};
19+
Queue.prototype.dequeue = function () {
20+
if (this.empty()) return;
21+
this.front = ++this.front % this.arr.length;
22+
this.size--;
23+
return this.arr[this.front];
24+
};
25+
Queue.prototype.front = function () {
26+
if (this.empty()) return;
27+
return this.arr[this.head];
28+
};
29+
Queue.prototype.full = function () {
30+
return this.size === this.arr.length;
31+
};
32+
Queue.prototype.empty = function () {
33+
return this.size === 0;
34+
};
35+
Queue.prototype.size = function () {
36+
return this.size;
37+
};
38+
Queue.prototype.clear = function () {
39+
this.front = this.tail = this.size = 0;
40+
};
41+
Queue.prototype.output = function () {
42+
let temp = [];
43+
for (let i = 0, j = this.front; i < this.size; i++) {
44+
temp.push(this.arr[j]);
45+
j++;
46+
if (j === this.arr.length) {
47+
j = 0;
48+
}
49+
}
50+
return temp;
51+
};
52+
53+
const queue = new Queue(3);
54+
queue.enqueue(1);
55+
queue.enqueue(2);
56+
queue.enqueue(3);
57+
queue.enqueue(4);
58+
// queue.dequeue();
59+
console.log(queue.output());
60+
console.log(queue.front, queue.tail);

‎栈和队列/创建队列.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Queue {
2+
constructor(n) {
3+
this.arr = new Array(n);
4+
this.size = 0;
5+
this.length = 0;
6+
this.front = 0;
7+
this.tail = 0;
8+
}
9+
}
10+
Queue.prototype.enqueue = function (val) {
11+
if (this.full()) return;
12+
this.arr[this.tail] = val;
13+
this.tail++;
14+
};
15+
Queue.prototype.dequeue = function () {
16+
if (this.empty()) return;
17+
this.front++;
18+
return this.arr[this.front];
19+
};
20+
Queue.prototype.front = function () {
21+
if (this.empty()) return;
22+
return this.arr[this.head];
23+
};
24+
Queue.prototype.full = function () {
25+
return this.tail === this.arr.length;
26+
};
27+
Queue.prototype.empty = function () {
28+
return this.tail === 0;
29+
};
30+
Queue.prototype.size = function () {
31+
return this.tail - this.head; // tail指向最后一个元素的下一个空位
32+
};
33+
Queue.prototype.output = function () {
34+
return this.arr.slice(this.front, this.tail);
35+
};
36+
37+
const queue = new Queue(10);
38+
queue.enqueue(1);
39+
queue.enqueue(2);
40+
queue.enqueue(3);
41+
queue.dequeue();
42+
console.log(queue.output());
43+
console.log(queue.front, queue.tail);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /