From 62114ce79ae83081b1abc3009b58b88f73684008 Mon Sep 17 00:00:00 2001 From: Justin Tse Date: Sat, 4 Feb 2023 21:26:14 +0800 Subject: [PATCH] Fix bug before commit 5eae708 (#325) * Fix bug before commit 5eae708 * Update queue.md --------- Co-authored-by: Yudong Jin --- .../chapter_stack_and_queue/array_queue.js | 6 ++++-- .../chapter_stack_and_queue/array_queue.ts | 8 +++++--- docs/chapter_stack_and_queue/queue.md | 14 +++++++++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/array_queue.js b/codes/javascript/chapter_stack_and_queue/array_queue.js index fc74a958e..c8d297186 100644 --- a/codes/javascript/chapter_stack_and_queue/array_queue.js +++ b/codes/javascript/chapter_stack_and_queue/array_queue.js @@ -31,8 +31,10 @@ class ArrayQueue { /* 入队 */ push(num) { - if (this.size == this.capacity) - throw new Error("队列已满"); + if (this.size == this.capacity) { + console.log("队列已满"); + return; + } // 计算尾指针,指向队尾索引 + 1 // 通过取余操作,实现 rear 越过数组尾部后回到头部 const rear = (this.#front + this.size) % this.capacity; diff --git a/codes/typescript/chapter_stack_and_queue/array_queue.ts b/codes/typescript/chapter_stack_and_queue/array_queue.ts index 814ea2512..a445a97a6 100644 --- a/codes/typescript/chapter_stack_and_queue/array_queue.ts +++ b/codes/typescript/chapter_stack_and_queue/array_queue.ts @@ -11,7 +11,7 @@ class ArrayQueue { private queSize: number; // 队列长度 constructor(capacity: number) { - this.nums = new Array(capacity); + this.nums = new Array(capacity); this.front = this.queSize = 0; } @@ -32,8 +32,10 @@ class ArrayQueue { /* 入队 */ push(num: number): void { - if (this.size == this.capacity) - throw new Error("队列已满"); + if (this.size == this.capacity) { + console.log("队列已满"); + return; + } // 计算尾指针,指向队尾索引 + 1 // 通过取余操作,实现 rear 越过数组尾部后回到头部 const rear = (this.front + this.queSize) % this.capacity; diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index d0ed36660..6eb6121e2 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -1027,8 +1027,10 @@ comments: true /* 入队 */ push(num) { - if (this.size == this.capacity) - throw new Error("队列已满"); + if (this.size == this.capacity) { + console.log("队列已满"); + return; + } // 计算尾指针,指向队尾索引 + 1 // 通过取余操作,实现 rear 越过数组尾部后回到头部 const rear = (this.#front + this.size) % this.capacity; @@ -1065,7 +1067,7 @@ comments: true private queSize: number; // 队列长度 constructor(capacity: number) { - this.nums = new Array(capacity); + this.nums = new Array(capacity); this.front = this.queSize = 0; } @@ -1086,8 +1088,10 @@ comments: true /* 入队 */ push(num: number): void { - if (this.size == this.capacity) - throw new Error("队列已满"); + if (this.size == this.capacity) { + console.log("队列已满"); + return; + } // 计算尾指针,指向队尾索引 + 1 // 通过取余操作,实现 rear 越过数组尾部后回到头部 const rear = (this.front + this.queSize) % this.capacity;