From 94797034e2c988398f9a10d3ff7966f31534d38f Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Mon, 19 Dec 2022 23:03:18 +0800 Subject: [PATCH 01/10] feat: add linkedlist_queue in ts --- .../linkedlist_queue.ts | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts new file mode 100644 index 00000000..01e93b06 --- /dev/null +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts @@ -0,0 +1,103 @@ +/** + * File: linkedlist_queue.ts + * Created Time: 2022-12-19 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + +import ListNode from "../module/ListNode" + +/* 基于链表实现的队列 */ +class LinkedListQueue { + private front: ListNode | null; + private rear: ListNode | null; // 头结点 front ,尾结点 rear + private queSize: number = 0; + + constructor() { + this.front = null; + this.rear = null; + } + + /* 获取队列的长度 */ + get size(): number { + return this.queSize; + } + + /* 判断队列是否为空 */ + isEmpty(): boolean { + return this.size === 0; + } + + /* 入队 */ + offer(num: number): void { + // 尾结点后添加 num + const node = new ListNode(num); + // 如果队列为空,则令头、尾结点都指向该结点 + if (!this.front) { + this.front = node; + this.rear = node; + // 如果队列不为空,则将该结点添加到尾结点后 + } else { + this.rear!.next = node; + this.rear = node; + } + this.queSize++; + } + + /* 出队 */ + poll(): number { + const num = this.peek(); + if (!this.front) { + throw new Error("No element in queue!") + } + // 删除头结点 + this.front = this.front.next; + this.queSize--; + return num; + } + + /* 访问队首元素 */ + peek(): number { + if (this.size === 0) + throw new Error("No element in queue!"); + return this.front!.val; + } + + /* 将链表转化为 Array 并返回 */ + toArray(): number[] { + let node = this.front; + const res = new Array(this.size); + for (let i = 0; i < res.length; i++) { + res[i] = node!.val; + node = node!.next; + } + return res; + } +} + + +/* 初始化队列 */ +const queue = new LinkedListQueue(); + +/* 元素入队 */ +queue.offer(1); +queue.offer(3); +queue.offer(2); +queue.offer(5); +queue.offer(4); +console.log("队列 queue = " + queue.toArray()); + +/* 访问队首元素 */ +const peek = queue.peek(); +console.log("队首元素 peek = " + peek); + +/* 元素出队 */ +const poll = queue.poll(); +console.log("出队元素 poll = " + poll + ",出队后 queue = " + queue.toArray()); + +/* 获取队列的长度 */ +const size = queue.size; +console.log("队列长度 size = " + size); + +/* 判断队列是否为空 */ +const isEmpty = queue.isEmpty(); +console.log("队列是否为空 = " + isEmpty); From 5d0249be1a106bf1e202f075d92e7a5bd4e73d08 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Mon, 19 Dec 2022 23:04:58 +0800 Subject: [PATCH 02/10] feat: add linkedlist_queue in ts to doc --- docs/chapter_stack_and_queue/queue.md | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index a13d7590..cdd52e1d 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -434,7 +434,73 @@ comments: true === "TypeScript" ```typescript title="linkedlist_queue.ts" + /* 基于链表实现的队列 */ + class LinkedListQueue { + private front: ListNode | null; + private rear: ListNode | null; // 头结点 front ,尾结点 rear + private queSize: number = 0; + constructor() { + this.front = null; + this.rear = null; + } + + /* 获取队列的长度 */ + get size(): number { + return this.queSize; + } + + /* 判断队列是否为空 */ + isEmpty(): boolean { + return this.size === 0; + } + + /* 入队 */ + offer(num: number): void { + // 尾结点后添加 num + const node = new ListNode(num); + // 如果队列为空,则令头、尾结点都指向该结点 + if (!this.front) { + this.front = node; + this.rear = node; + // 如果队列不为空,则将该结点添加到尾结点后 + } else { + this.rear!.next = node; + this.rear = node; + } + this.queSize++; + } + + /* 出队 */ + poll(): number { + const num = this.peek(); + if (!this.front) { + throw new Error("No element in queue!") + } + // 删除头结点 + this.front = this.front.next; + this.queSize--; + return num; + } + + /* 访问队首元素 */ + peek(): number { + if (this.size === 0) + throw new Error("No element in queue!"); + return this.front!.val; + } + + /* 将链表转化为 Array 并返回 */ + toArray(): number[] { + let node = this.front; + const res = new Array(this.size); + for (let i = 0; i < res.length; i++) { + res[i] = node!.val; + node = node!.next; + } + return res; + } + } ``` === "C" From c79c26550d24c3d965fc55756acd3991aaf834f7 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Mon, 12 Dec 2022 22:12:18 +0800 Subject: [PATCH 03/10] feat: add array_queue in ts --- .../chapter_stack_and_queue/array_queue.ts | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 codes/typescript/chapter_stack_and_queue/array_queue.ts diff --git a/codes/typescript/chapter_stack_and_queue/array_queue.ts b/codes/typescript/chapter_stack_and_queue/array_queue.ts new file mode 100644 index 00000000..c232335d --- /dev/null +++ b/codes/typescript/chapter_stack_and_queue/array_queue.ts @@ -0,0 +1,123 @@ +/** + * File: array_queue.ts + * Created Time: 2022-12-11 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + + +/* 基于环形数组实现的队列 */ +class ArrayQueue { + private queue: number[]; // 用于存储队列元素的数组 + private front: number = 0; // 头指针,指向队首 + private rear: number = 0; // 尾指针,指向队尾 + 1 + private CAPACITY: number = 1e5; + + constructor(capacity?: number) { + this.queue = new Array(capacity ?? this.CAPACITY); + } + + /* 获取队列的容量 */ + get capacity(): number { + return this.queue.length; + } + + /* 获取队列的长度 */ + get size(): number { + // 由于将数组看作为环形,可能 rear < front ,因此需要取余数 + return (this.capacity + this.rear - this.front) % this.capacity; + } + + /* 判断队列是否为空 */ + empty(): boolean { + return this.rear - this.front == 0; + } + + /* 入队 */ + offer(num: number): void { + if (this.size == this.capacity) { + console.log("队列已满"); + return; + } + // 尾结点后添加 num + this.queue[this.rear] = num; + // 尾指针向后移动一位,越过尾部后返回到数组头部 + this.rear = (this.rear + 1) % this.capacity; + } + + /* 出队 */ + poll(): number { + const num = this.peek(); + // 队头指针向后移动一位,若越过尾部则返回到数组头部 + this.front = (this.front + 1) % this.capacity; + return num; + } + + /* 访问队首元素 */ + peek(): number { + // 删除头结点 + if (this.empty()) + throw new Error("The queue is empty!"); + return this.queue[this.front]; + } + + /* 访问指定索引元素 */ + get(index: number): number { + if (index >= this.size) + throw new Error("Index out of bounds!"); + return this.queue[(this.front + index) % this.capacity]; + } + + /* 返回 Array */ + toArray(): number[] { + const siz = this.size; + const cap = this.capacity; + // 仅转换有效长度范围内的列表元素 + const arr = new Array(siz); + for (let i = 0, j = this.front; i < siz; i++, j++) { + arr[i] = this.queue[j % cap]; + } + return arr; + } +} + +/* 初始化队列 */ +const capacity = 10; +const queue = new ArrayQueue(capacity); + +/* 元素入队 */ +queue.offer(1); +queue.offer(3); +queue.offer(2); +queue.offer(5); +queue.offer(4); +console.log("队列 queue = "); +console.log(queue.toArray()); + +/* 访问队首元素 */ +const peek = queue.peek(); +console.log("队首元素 peek = " + peek); + +/* 访问指定索引元素 */ +const num = queue.get(2); +console.log("队列第 3 个元素为 num = " + num); + +/* 元素出队 */ +const poll = queue.poll(); +console.log("出队元素 poll = " + poll + ",出队后 queue = "); +console.log(queue.toArray()); + +/* 获取队列的长度 */ +const size = queue.size; +console.log("队列长度 size = " + size); + +/* 判断队列是否为空 */ +const empty = queue.empty(); +console.log("队列是否为空 = " + empty); + +/* 测试环形数组 */ +for (let i = 0; i < 10; i++) { + queue.offer(i); + queue.poll(); + console.log("第 " + i + " 轮入队 + 出队后 queue = "); + console.log(queue.toArray()); +} From 137d241d979e583651501bdbef39b9713eec31b6 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Mon, 12 Dec 2022 22:13:10 +0800 Subject: [PATCH 04/10] feat: complement array_queue doc in ts --- docs/chapter_stack_and_queue/queue.md | 117 ++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index cdd52e1d..8b096c49 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -790,6 +790,123 @@ comments: true === "TypeScript" ```typescript title="array_queue.ts" + + /* 基于环形数组实现的队列 */ + class ArrayQueue { + private queue: number[]; // 用于存储队列元素的数组 + private front: number = 0; // 头指针,指向队首 + private rear: number = 0; // 尾指针,指向队尾 + 1 + private CAPACITY: number = 1e5; + + constructor(capacity?: number) { + this.queue = new Array(capacity ?? this.CAPACITY); + } + + /* 获取队列的容量 */ + get capacity(): number { + return this.queue.length; + } + + /* 获取队列的长度 */ + get size(): number { + // 由于将数组看作为环形,可能 rear < front ,因此需要取余数 + return (this.capacity + this.rear - this.front) % this.capacity; + } + + /* 判断队列是否为空 */ + empty(): boolean { + return this.rear - this.front == 0; + } + + /* 入队 */ + offer(num: number): void { + if (this.size == this.capacity) { + console.log("队列已满"); + return; + } + // 尾结点后添加 num + this.queue[this.rear] = num; + // 尾指针向后移动一位,越过尾部后返回到数组头部 + this.rear = (this.rear + 1) % this.capacity; + } + + /* 出队 */ + poll(): number { + const num = this.peek(); + // 队头指针向后移动一位,若越过尾部则返回到数组头部 + this.front = (this.front + 1) % this.capacity; + return num; + } + + /* 访问队首元素 */ + peek(): number { + // 删除头结点 + if (this.empty()) + throw new Error("The queue is empty!"); + return this.queue[this.front]; + } + + /* 访问指定索引元素 */ + get(index: number): number { + if (index >= this.size) + throw new Error("Index out of bounds!"); + return this.queue[(this.front + index) % this.capacity]; + } + + /* 返回 Array */ + toArray(): number[] { + const siz = this.size; + const cap = this.capacity; + // 仅转换有效长度范围内的列表元素 + const arr = new Array(siz); + for (let i = 0, j = this.front; i < siz; i++, j++) { + arr[i] = this.queue[j % cap]; + } + return arr; + } + } + + /* 初始化队列 */ + const capacity = 10; + const queue = new ArrayQueue(capacity); + + /* 元素入队 */ + queue.offer(1); + queue.offer(3); + queue.offer(2); + queue.offer(5); + queue.offer(4); + console.log("队列 queue = "); + console.log(queue.toArray()); + + /* 访问队首元素 */ + const peek = queue.peek(); + console.log("队首元素 peek = " + peek); + + /* 访问指定索引元素 */ + const num = queue.get(2); + console.log("队列第 3 个元素为 num = " + num); + + /* 元素出队 */ + const poll = queue.poll(); + console.log("出队元素 poll = " + poll + ",出队后 queue = "); + console.log(queue.toArray()); + + /* 获取队列的长度 */ + const size = queue.size; + console.log("队列长度 size = " + size); + + /* 判断队列是否为空 */ + const empty = queue.empty(); + console.log("队列是否为空 = " + empty); + + /* 测试环形数组 */ + for (let i = 0; i < 10; i++) { + queue.offer(i); + queue.poll(); + console.log("第 " + i + " 轮入队 + 出队后 queue = "); + console.log(queue.toArray()); + } ``` From 14561fc95fc3f6fdd37a511757cf7057fd61b93b Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Tue, 13 Dec 2022 00:00:06 +0800 Subject: [PATCH 05/10] feat: add array_queue in js --- .../chapter_stack_and_queue/array_queue.js | 123 ++++++++++++++++++ .../chapter_stack_and_queue/array_queue.ts | 2 + 2 files changed, 125 insertions(+) create mode 100644 codes/javascript/chapter_stack_and_queue/array_queue.js diff --git a/codes/javascript/chapter_stack_and_queue/array_queue.js b/codes/javascript/chapter_stack_and_queue/array_queue.js new file mode 100644 index 00000000..fa84fcea --- /dev/null +++ b/codes/javascript/chapter_stack_and_queue/array_queue.js @@ -0,0 +1,123 @@ +/** + * File: array_queue.js + * Created Time: 2022-12-13 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + + +/* 基于环形数组实现的队列 */ +class ArrayQueue { + queue; // 用于存储队列元素的数组 + front = 0; // 头指针,指向队首 + rear = 0; // 尾指针,指向队尾 + 1 + CAPACITY = 1e5; + + constructor(capacity) { + this.queue = new Array(capacity ?? this.CAPACITY); + } + + /* 获取队列的容量 */ + get capacity() { + return this.queue.length; + } + + /* 获取队列的长度 */ + get size() { + // 由于将数组看作为环形,可能 rear < front ,因此需要取余数 + return (this.capacity + this.rear - this.front) % this.capacity; + } + + /* 判断队列是否为空 */ + empty() { + return this.rear - this.front == 0; + } + + /* 入队 */ + offer(num) { + if (this.size == this.capacity) { + console.log("队列已满"); + return; + } + // 尾结点后添加 num + this.queue[this.rear] = num; + // 尾指针向后移动一位,越过尾部后返回到数组头部 + this.rear = (this.rear + 1) % this.capacity; + } + + /* 出队 */ + poll() { + const num = this.peek(); + // 队头指针向后移动一位,若越过尾部则返回到数组头部 + this.front = (this.front + 1) % this.capacity; + return num; + } + + /* 访问队首元素 */ + peek() { + // 删除头结点 + if (this.empty()) + throw new Error("The queue is empty!"); + return this.queue[this.front]; + } + + /* 访问指定索引元素 */ + get(index) { + if (index >= this.size) + throw new Error("Index out of bounds!"); + return this.queue[(this.front + index) % this.capacity]; + } + + /* 返回 Array */ + toArray() { + const siz = this.size; + const cap = this.capacity; + // 仅转换有效长度范围内的列表元素 + const arr = new Array(siz); + for (let i = 0, j = this.front; i < siz; i++, j++) { + arr[i] = this.queue[j % cap]; + } + return arr; + } +} + +/* 初始化队列 */ +const capacity = 10; +const queue = new ArrayQueue(capacity); + +/* 元素入队 */ +queue.offer(1); +queue.offer(3); +queue.offer(2); +queue.offer(5); +queue.offer(4); +console.log("队列 queue = "); +console.log(queue.toArray()); + +/* 访问队首元素 */ +const peek = queue.peek(); +console.log("队首元素 peek = " + peek); + +/* 访问指定索引元素 */ +const num = queue.get(2); +console.log("队列第 3 个元素为 num = " + num); + +/* 元素出队 */ +const poll = queue.poll(); +console.log("出队元素 poll = " + poll + ",出队后 queue = "); +console.log(queue.toArray()); + +/* 获取队列的长度 */ +const size = queue.size; +console.log("队列长度 size = " + size); + +/* 判断队列是否为空 */ +const empty = queue.empty(); +console.log("队列是否为空 = " + empty); + +/* 测试环形数组 */ +for (let i = 0; i < 10; i++) { + queue.offer(i); + queue.poll(); + console.log("第 " + i + " 轮入队 + 出队后 queue = "); + console.log(queue.toArray()); +} diff --git a/codes/typescript/chapter_stack_and_queue/array_queue.ts b/codes/typescript/chapter_stack_and_queue/array_queue.ts index c232335d..789d2d99 100644 --- a/codes/typescript/chapter_stack_and_queue/array_queue.ts +++ b/codes/typescript/chapter_stack_and_queue/array_queue.ts @@ -121,3 +121,5 @@ for (let i = 0; i < 10; i++) { console.log("第 " + i + " 轮入队 + 出队后 queue = "); console.log(queue.toArray()); } + +export { }; \ No newline at end of file From 113083c1c7b6e938fcc5bd12c9a81b373ce0688f Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Tue, 13 Dec 2022 00:01:03 +0800 Subject: [PATCH 06/10] feat: complement array_queue doc in js --- docs/chapter_stack_and_queue/queue.md | 75 ++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 8b096c49..a46a2f42 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -784,13 +784,86 @@ comments: true === "JavaScript" ```js title="array_queue.js" + /* 基于环形数组实现的队列 */ + class ArrayQueue { + queue; // 用于存储队列元素的数组 + front = 0; // 头指针,指向队首 + rear = 0; // 尾指针,指向队尾 + 1 + CAPACITY = 1e5; + constructor(capacity) { + this.queue = new Array(capacity ?? this.CAPACITY); + } + + /* 获取队列的容量 */ + get capacity() { + return this.queue.length; + } + + /* 获取队列的长度 */ + get size() { + // 由于将数组看作为环形,可能 rear < front ,因此需要取余数 + return (this.capacity + this.rear - this.front) % this.capacity; + } + + /* 判断队列是否为空 */ + empty() { + return this.rear - this.front == 0; + } + + /* 入队 */ + offer(num) { + if (this.size == this.capacity) { + console.log("队列已满"); + return; + } + // 尾结点后添加 num + this.queue[this.rear] = num; + // 尾指针向后移动一位,越过尾部后返回到数组头部 + this.rear = (this.rear + 1) % this.capacity; + } + + /* 出队 */ + poll() { + const num = this.peek(); + // 队头指针向后移动一位,若越过尾部则返回到数组头部 + this.front = (this.front + 1) % this.capacity; + return num; + } + + /* 访问队首元素 */ + peek() { + // 删除头结点 + if (this.empty()) + throw new Error("The queue is empty!"); + return this.queue[this.front]; + } + + /* 访问指定索引元素 */ + get(index) { + if (index >= this.size) + throw new Error("Index out of bounds!"); + return this.queue[(this.front + index) % this.capacity]; + } + + /* 返回 Array */ + toArray() { + const siz = this.size; + const cap = this.capacity; + // 仅转换有效长度范围内的列表元素 + const arr = new Array(siz); + for (let i = 0, j = this.front; i < siz; i++, j++) { + arr[i] = this.queue[j % cap]; + } + return arr; + } + } ``` === "TypeScript" ```typescript title="array_queue.ts" - + /* 基于环形数组实现的队列 */ class ArrayQueue { private queue: number[]; // 用于存储队列元素的数组 From e2aec30b05bf62e78c2563307873f33b730b5298 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Wed, 14 Dec 2022 00:02:25 +0800 Subject: [PATCH 07/10] fix: use private variables --- .../chapter_stack_and_queue/array_queue.js | 30 ++++++------- docs/chapter_stack_and_queue/queue.md | 43 ------------------- 2 files changed, 15 insertions(+), 58 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/array_queue.js b/codes/javascript/chapter_stack_and_queue/array_queue.js index fa84fcea..89234952 100644 --- a/codes/javascript/chapter_stack_and_queue/array_queue.js +++ b/codes/javascript/chapter_stack_and_queue/array_queue.js @@ -7,29 +7,29 @@ /* 基于环形数组实现的队列 */ class ArrayQueue { - queue; // 用于存储队列元素的数组 - front = 0; // 头指针,指向队首 - rear = 0; // 尾指针,指向队尾 + 1 - CAPACITY = 1e5; + #queue; // 用于存储队列元素的数组 + #front = 0; // 头指针,指向队首 + #rear = 0; // 尾指针,指向队尾 + 1 + #CAPACITY = 1e5; constructor(capacity) { - this.queue = new Array(capacity ?? this.CAPACITY); + this.#queue = new Array(capacity ?? this.CAPACITY); } /* 获取队列的容量 */ get capacity() { - return this.queue.length; + return this.#queue.length; } /* 获取队列的长度 */ get size() { // 由于将数组看作为环形,可能 rear < front ,因此需要取余数 - return (this.capacity + this.rear - this.front) % this.capacity; + return (this.capacity + this.#rear - this.#front) % this.capacity; } /* 判断队列是否为空 */ empty() { - return this.rear - this.front == 0; + return this.#rear - this.#front == 0; } /* 入队 */ @@ -39,16 +39,16 @@ class ArrayQueue { return; } // 尾结点后添加 num - this.queue[this.rear] = num; + this.#queue[this.#rear] = num; // 尾指针向后移动一位,越过尾部后返回到数组头部 - this.rear = (this.rear + 1) % this.capacity; + this.#rear = (this.#rear + 1) % this.capacity; } /* 出队 */ poll() { const num = this.peek(); // 队头指针向后移动一位,若越过尾部则返回到数组头部 - this.front = (this.front + 1) % this.capacity; + this.#front = (this.#front + 1) % this.capacity; return num; } @@ -57,14 +57,14 @@ class ArrayQueue { // 删除头结点 if (this.empty()) throw new Error("The queue is empty!"); - return this.queue[this.front]; + return this.#queue[this.#front]; } /* 访问指定索引元素 */ get(index) { if (index >= this.size) throw new Error("Index out of bounds!"); - return this.queue[(this.front + index) % this.capacity]; + return this.#queue[(this.#front + index) % this.capacity]; } /* 返回 Array */ @@ -73,8 +73,8 @@ class ArrayQueue { const cap = this.capacity; // 仅转换有效长度范围内的列表元素 const arr = new Array(siz); - for (let i = 0, j = this.front; i < siz; i++, j++) { - arr[i] = this.queue[j % cap]; + for (let i = 0, j = this.#front; i < siz; i++, j++) { + arr[i] = this.#queue[j % cap]; } return arr; } diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index a46a2f42..327a3edc 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -938,49 +938,6 @@ comments: true return arr; } } - - /* 初始化队列 */ - const capacity = 10; - const queue = new ArrayQueue(capacity); - - /* 元素入队 */ - queue.offer(1); - queue.offer(3); - queue.offer(2); - queue.offer(5); - queue.offer(4); - console.log("队列 queue = "); - console.log(queue.toArray()); - - /* 访问队首元素 */ - const peek = queue.peek(); - console.log("队首元素 peek = " + peek); - - /* 访问指定索引元素 */ - const num = queue.get(2); - console.log("队列第 3 个元素为 num = " + num); - - /* 元素出队 */ - const poll = queue.poll(); - console.log("出队元素 poll = " + poll + ",出队后 queue = "); - console.log(queue.toArray()); - - /* 获取队列的长度 */ - const size = queue.size; - console.log("队列长度 size = " + size); - - /* 判断队列是否为空 */ - const empty = queue.empty(); - console.log("队列是否为空 = " + empty); - - /* 测试环形数组 */ - for (let i = 0; i < 10; i++) { - queue.offer(i); - queue.poll(); - console.log("第 " + i + " 轮入队 + 出队后 queue = "); - console.log(queue.toArray()); - } - ``` === "C" From 1f118c24076bc92fe743aefbb7f3dadc6fb9df8e Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Tue, 20 Dec 2022 00:00:50 +0800 Subject: [PATCH 08/10] feat: add linkedlist_queue in js --- .../linkedlist_queue.js | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 codes/javascript/chapter_stack_and_queue/linkedlist_queue.js diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js b/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js new file mode 100644 index 00000000..a05b10b9 --- /dev/null +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js @@ -0,0 +1,103 @@ +/** + * File: linkedlist_queue.js + * Created Time: 2022-12-20 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + +const ListNode = require("../include/ListNode"); + +/* 基于链表实现的队列 */ +class LinkedListQueue { + #front; + #rear; // 头结点 #front ,尾结点 #rear + #queSize = 0; + + constructor() { + this.#front = null; + this.#rear = null; + } + + /* 获取队列的长度 */ + get size() { + return this.#queSize; + } + + /* 判断队列是否为空 */ + isEmpty() { + return this.size === 0; + } + + /* 入队 */ + offer(num) { + // 尾结点后添加 num + const node = new ListNode(num); + // 如果队列为空,则令头、尾结点都指向该结点 + if (!this.#front) { + this.#front = node; + this.#rear = node; + // 如果队列不为空,则将该结点添加到尾结点后 + } else { + this.#rear.next = node; + this.#rear = node; + } + this.#queSize++; + } + + /* 出队 */ + poll() { + const num = this.peek(); + if (!this.#front) { + throw new Error("No element in queue!") + } + // 删除头结点 + this.#front = this.#front.next; + this.#queSize--; + return num; + } + + /* 访问队首元素 */ + peek() { + if (this.size === 0) + throw new Error("No element in queue!"); + return this.#front.val; + } + + /* 将链表转化为 Array 并返回 */ + toArray() { + let node = this.#front; + const res = new Array(this.size); + for (let i = 0; i < res.length; i++) { + res[i] = node.val; + node = node.next; + } + return res; + } +} + + +/* 初始化队列 */ +const queue = new LinkedListQueue(); + +/* 元素入队 */ +queue.offer(1); +queue.offer(3); +queue.offer(2); +queue.offer(5); +queue.offer(4); +console.log("队列 queue = " + queue.toArray()); + +/* 访问队首元素 */ +const peek = queue.peek(); +console.log("队首元素 peek = " + peek); + +/* 元素出队 */ +const poll = queue.poll(); +console.log("出队元素 poll = " + poll + ",出队后 queue = " + queue.toArray()); + +/* 获取队列的长度 */ +const size = queue.size; +console.log("队列长度 size = " + size); + +/* 判断队列是否为空 */ +const isEmpty = queue.isEmpty(); +console.log("队列是否为空 = " + isEmpty); From 6e715728ee6d1e61c9af1df0b61d5ff0b6a75187 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Tue, 20 Dec 2022 00:01:29 +0800 Subject: [PATCH 09/10] feat: add linkedlist_queue in js to doc --- docs/chapter_stack_and_queue/queue.md | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 327a3edc..820eefad 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -428,7 +428,72 @@ comments: true === "JavaScript" ```js title="linkedlist_queue.js" + class LinkedListQueue { + #front; + #rear; // 头结点 #front ,尾结点 #rear + #queSize = 0; + constructor() { + this.#front = null; + this.#rear = null; + } + + /* 获取队列的长度 */ + get size() { + return this.#queSize; + } + + /* 判断队列是否为空 */ + isEmpty() { + return this.size === 0; + } + + /* 入队 */ + offer(num) { + // 尾结点后添加 num + const node = new ListNode(num); + // 如果队列为空,则令头、尾结点都指向该结点 + if (!this.#front) { + this.#front = node; + this.#rear = node; + // 如果队列不为空,则将该结点添加到尾结点后 + } else { + this.#rear.next = node; + this.#rear = node; + } + this.#queSize++; + } + + /* 出队 */ + poll() { + const num = this.peek(); + if (!this.#front) { + throw new Error("No element in queue!") + } + // 删除头结点 + this.#front = this.#front.next; + this.#queSize--; + return num; + } + + /* 访问队首元素 */ + peek() { + if (this.size === 0) + throw new Error("No element in queue!"); + return this.#front.val; + } + + /* 将链表转化为 Array 并返回 */ + toArray() { + let node = this.#front; + const res = new Array(this.size); + for (let i = 0; i < res.length; i++) { + res[i] = node.val; + node = node.next; + } + return res; + } + } ``` === "TypeScript" From edf100ec01c379589b1d3842d58a49570b40ba3a Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 20 Dec 2022 21:02:09 +0800 Subject: [PATCH 10/10] Fine tune. --- .../chapter_stack_and_queue/array_queue.js | 20 +++++++++---------- .../linkedlist_queue.js | 4 ++-- .../chapter_stack_and_queue/queue.js | 4 +++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/array_queue.js b/codes/javascript/chapter_stack_and_queue/array_queue.js index 89234952..902c7407 100644 --- a/codes/javascript/chapter_stack_and_queue/array_queue.js +++ b/codes/javascript/chapter_stack_and_queue/array_queue.js @@ -7,10 +7,10 @@ /* 基于环形数组实现的队列 */ class ArrayQueue { - #queue; // 用于存储队列元素的数组 - #front = 0; // 头指针,指向队首 - #rear = 0; // 尾指针,指向队尾 + 1 - #CAPACITY = 1e5; + #queue; // 用于存储队列元素的数组 + #front = 0; // 头指针,指向队首 + #rear = 0; // 尾指针,指向队尾 + 1 + #CAPACITY = 1e3; // 默认初始容量 constructor(capacity) { this.#queue = new Array(capacity ?? this.CAPACITY); @@ -34,10 +34,8 @@ class ArrayQueue { /* 入队 */ offer(num) { - if (this.size == this.capacity) { - console.log("队列已满"); - return; - } + if (this.size == this.capacity) + throw new Error("队列已满"); // 尾结点后添加 num this.#queue[this.#rear] = num; // 尾指针向后移动一位,越过尾部后返回到数组头部 @@ -56,14 +54,14 @@ class ArrayQueue { peek() { // 删除头结点 if (this.empty()) - throw new Error("The queue is empty!"); + throw new Error("队列为空"); return this.#queue[this.#front]; } /* 访问指定索引元素 */ get(index) { if (index >= this.size) - throw new Error("Index out of bounds!"); + throw new Error("索引越界"); return this.#queue[(this.#front + index) % this.capacity]; } @@ -80,6 +78,8 @@ class ArrayQueue { } } + +/* Driver Code */ /* 初始化队列 */ const capacity = 10; const queue = new ArrayQueue(capacity); diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js b/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js index a05b10b9..cf1f3b3d 100644 --- a/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js @@ -47,7 +47,7 @@ class LinkedListQueue { poll() { const num = this.peek(); if (!this.#front) { - throw new Error("No element in queue!") + throw new Error("队列为空") } // 删除头结点 this.#front = this.#front.next; @@ -58,7 +58,7 @@ class LinkedListQueue { /* 访问队首元素 */ peek() { if (this.size === 0) - throw new Error("No element in queue!"); + throw new Error("队列为空"); return this.#front.val; } diff --git a/codes/javascript/chapter_stack_and_queue/queue.js b/codes/javascript/chapter_stack_and_queue/queue.js index 5d26727f..7d4aeeee 100644 --- a/codes/javascript/chapter_stack_and_queue/queue.js +++ b/codes/javascript/chapter_stack_and_queue/queue.js @@ -4,6 +4,8 @@ * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) */ + +/* Driver Code */ /* 初始化队列 */ // JavaScript 没有内置的队列,可以把 Array 当作队列来使用 // 注意:由于是数组,所以 shift() 的时间复杂度是 O(n) @@ -20,7 +22,7 @@ queue.push(4); const peek = queue[0]; /* 元素出队 */ -// O(n) +// shift() 方法的时间复杂度为 O(n) const poll = queue.shift(); /* 获取队列的长度 */