mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-10 17:00:45 +08:00
Fomrat the JS and TS codes with prettier.
This commit is contained in:
@ -6,9 +6,9 @@
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
#nums; // 用于存储队列元素的数组
|
||||
#front = 0; // 队首指针,指向队首元素
|
||||
#queSize = 0; // 队列长度
|
||||
#nums; // 用于存储队列元素的数组
|
||||
#front = 0; // 队首指针,指向队首元素
|
||||
#queSize = 0; // 队列长度
|
||||
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
@ -32,7 +32,7 @@ class ArrayQueue {
|
||||
/* 入队 */
|
||||
push(num) {
|
||||
if (this.size == this.capacity) {
|
||||
console.log("队列已满");
|
||||
console.log('队列已满');
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
@ -54,8 +54,7 @@ class ArrayQueue {
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek() {
|
||||
if (this.empty())
|
||||
throw new Error("队列为空");
|
||||
if (this.empty()) throw new Error('队列为空');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
@ -70,7 +69,6 @@ class ArrayQueue {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化队列 */
|
||||
const capacity = 10;
|
||||
@ -82,27 +80,27 @@ queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log("队列 queue =", queue.toArray());
|
||||
console.log('队列 queue =', queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue.peek();
|
||||
console.log("队首元素 peek = " + peek);
|
||||
console.log('队首元素 peek = ' + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
const pop = queue.pop();
|
||||
console.log("出队元素 pop = " + pop + ",出队后 queue =", queue.toArray());
|
||||
console.log('出队元素 pop = ' + pop + ',出队后 queue =', queue.toArray());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.size;
|
||||
console.log("队列长度 size = " + size);
|
||||
console.log('队列长度 size = ' + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.empty();
|
||||
console.log("队列是否为空 = " + empty);
|
||||
console.log('队列是否为空 = ' + empty);
|
||||
|
||||
/* 测试环形数组 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
console.log("第 " + i + " 轮入队 + 出队后 queue =", queue.toArray());
|
||||
console.log('第 ' + i + ' 轮入队 + 出队后 queue =', queue.toArray());
|
||||
}
|
||||
|
Reference in New Issue
Block a user