From 23bf19b7fa8511abb37389c4727a2cf32bca40b7 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Wed, 21 Dec 2022 16:22:58 +0800 Subject: [PATCH 1/7] feat: add linkedlist_stack in ts --- .../linkedlist_stack.ts | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts new file mode 100644 index 0000000000..0e8b46bc95 --- /dev/null +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts @@ -0,0 +1,93 @@ +/** + * File: linkedlist_queue.ts + * Created Time: 2022-12-21 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + +import ListNode from "../module/ListNode" + +/* 基于链表实现的栈 */ +class LinkedListStack { + private stackPeek: ListNode | null; // 将头结点作为栈顶 + private stkSize: number = 0; // 栈的长度 + + constructor() { + this.stackPeek = null; + } + + /* 获取栈的长度 */ + get size(): number { + return this.stkSize; + } + + /* 判断栈是否为空 */ + isEmpty(): boolean { + return this.size == 0; + } + + /* 入栈 */ + push(num: number): void { + const node = new ListNode(num); + node.next = this.stackPeek; + this.stackPeek = node; + this.stkSize++; + } + + /* 出栈 */ + pop(): number { + const num = this.peek(); + if (!this.stackPeek) { + throw new Error("栈为空!"); + } + this.stackPeek = this.stackPeek.next; + this.stkSize--; + return num; + } + + /* 访问栈顶元素 */ + peek(): number { + if (!this.stackPeek) { + throw new Error("栈为空!"); + } + return this.stackPeek.val; + } + + /* 将 List 转化为 Array 并返回 */ + toArray(): number[] { + let node = this.stackPeek; + const res = new Array(this.size); + for (let i = res.length - 1; i >= 0; i--) { + res[i] = node!.val; + node = node!.next; + } + return res; + } +} + + +/* 初始化栈 */ +const stack = new LinkedListStack(); + +/* 元素入栈 */ +stack.push(1); +stack.push(3); +stack.push(2); +stack.push(5); +stack.push(4); +console.log("栈 stack = " + stack.toArray()); + +/* 访问栈顶元素 */ +const peek = stack.peek(); +console.log("栈顶元素 peek = " + peek); + +/* 元素出栈 */ +const pop = stack.pop(); +console.log("出栈元素 pop = " + pop + ",出栈后 stack = " + stack.toArray()); + +/* 获取栈的长度 */ +const size = stack.size; +console.log("栈的长度 size = " + size); + +/* 判断是否为空 */ +const isEmpty = stack.isEmpty(); +console.log("栈是否为空 = " + isEmpty); From 5856e6a173760847a70404f94b6aff486c6748c5 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Thu, 22 Dec 2022 09:29:55 +0800 Subject: [PATCH 2/7] feat: add linkedlist_stack in ts to doc --- docs/chapter_stack_and_queue/stack.md | 57 ++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 63d7353e2c..cbeac30f5c 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -396,7 +396,62 @@ comments: true === "TypeScript" ```typescript title="linkedlist_stack.ts" - + class LinkedListStack { + private stackPeek: ListNode | null; // 将头结点作为栈顶 + private stkSize: number = 0; // 栈的长度 + + constructor() { + this.stackPeek = null; + } + + /* 获取栈的长度 */ + get size(): number { + return this.stkSize; + } + + /* 判断栈是否为空 */ + isEmpty(): boolean { + return this.size == 0; + } + + /* 入栈 */ + push(num: number): void { + const node = new ListNode(num); + node.next = this.stackPeek; + this.stackPeek = node; + this.stkSize++; + } + + /* 出栈 */ + pop(): number { + const num = this.peek(); + if (!this.stackPeek) { + throw new Error("栈为空!"); + } + this.stackPeek = this.stackPeek.next; + this.stkSize--; + return num; + } + + /* 访问栈顶元素 */ + peek(): number { + if (!this.stackPeek) { + throw new Error("栈为空!"); + } + return this.stackPeek.val; + } + + /* 将 List 转化为 Array 并返回 */ + toArray(): number[] { + let node = this.stackPeek; + const res = new Array(this.size); + for (let i = res.length - 1; i >= 0; i--) { + res[i] = node!.val; + node = node!.next; + } + return res; + } + } ``` === "C" From 0394f0c547f25244a1fbb3edccd3c8b6069f83cb Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Fri, 23 Dec 2022 11:48:21 +0800 Subject: [PATCH 3/7] feat: add linkedlist_stack in js --- .../linkedlist_stack.js | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 codes/javascript/chapter_stack_and_queue/linkedlist_stack.js diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js new file mode 100644 index 0000000000..9c86f905a9 --- /dev/null +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js @@ -0,0 +1,93 @@ +/**#stkSize + * File: linkedlist_queue.js + * Created Time: 2022-12-22 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + +const ListNode = require("../include/ListNode"); + +/* 基于链表实现的栈 */ +class LinkedListStack { + #stackPeek; // 将头结点作为栈顶 + #stkSize = 0; // 栈的长度 + + constructor() { + this.#stackPeek = null; + } + + /* 获取栈的长度 */ + get size() { + return this.#stkSize; + } + + /* 判断栈是否为空 */ + isEmpty() { + return this.size == 0; + } + + /* 入栈 */ + push(num) { + const node = new ListNode(num); + node.next = this.#stackPeek; + this.#stackPeek = node; + this.#stkSize++; + } + + /* 出栈 */ + pop() { + const num = this.peek(); + if (!this.#stackPeek) { + throw new Error("栈为空!"); + } + this.#stackPeek = this.#stackPeek.next; + this.#stkSize--; + return num; + } + + /* 访问栈顶元素 */ + peek() { + if (!this.#stackPeek) { + throw new Error("栈为空!"); + } + return this.#stackPeek.val; + } + + /* 将 List 转化为 Array 并返回 */ + toArray() { + let node = this.#stackPeek; + const res = new Array(this.size); + for (let i = res.length - 1; i >= 0; i--) { + res[i] = node.val; + node = node.next; + } + return res; + } +} + + +/* 初始化栈 */ +const stack = new LinkedListStack(); + +/* 元素入栈 */ +stack.push(1); +stack.push(3); +stack.push(2); +stack.push(5); +stack.push(4); +console.log("栈 stack = " + stack.toArray()); + +/* 访问栈顶元素 */ +const peek = stack.peek(); +console.log("栈顶元素 peek = " + peek); + +/* 元素出栈 */ +const pop = stack.pop(); +console.log("出栈元素 pop = " + pop + ",出栈后 stack = " + stack.toArray()); + +/* 获取栈的长度 */ +const size = stack.size; +console.log("栈的长度 size = " + size); + +/* 判断是否为空 */ +const isEmpty = stack.isEmpty(); +console.log("栈是否为空 = " + isEmpty); From c20f7cdaac07f2b7c0cf20718f475fb63356e274 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Fri, 23 Dec 2022 12:08:15 +0800 Subject: [PATCH 4/7] fix: fix some comment --- codes/javascript/chapter_stack_and_queue/linkedlist_stack.js | 4 ++-- codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts | 4 ++-- docs/chapter_stack_and_queue/stack.md | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js index 9c86f905a9..15707a8d38 100644 --- a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js @@ -1,5 +1,5 @@ /**#stkSize - * File: linkedlist_queue.js + * File: linkedlist_stack.js * Created Time: 2022-12-22 * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) */ @@ -52,7 +52,7 @@ class LinkedListStack { return this.#stackPeek.val; } - /* 将 List 转化为 Array 并返回 */ + /* 将链表转化为 Array 并返回 */ toArray() { let node = this.#stackPeek; const res = new Array(this.size); diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts index 0e8b46bc95..cdc4878947 100644 --- a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts @@ -1,5 +1,5 @@ /** - * File: linkedlist_queue.ts + * File: linkedlist_stack.ts * Created Time: 2022-12-21 * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) */ @@ -52,7 +52,7 @@ class LinkedListStack { return this.stackPeek.val; } - /* 将 List 转化为 Array 并返回 */ + /* 将链表转化为 Array 并返回 */ toArray(): number[] { let node = this.stackPeek; const res = new Array(this.size); diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index cbeac30f5c..c8149021a3 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -396,6 +396,7 @@ comments: true === "TypeScript" ```typescript title="linkedlist_stack.ts" + /* 基于链表实现的栈 */ class LinkedListStack { private stackPeek: ListNode | null; // 将头结点作为栈顶 private stkSize: number = 0; // 栈的长度 @@ -441,7 +442,7 @@ comments: true return this.stackPeek.val; } - /* 将 List 转化为 Array 并返回 */ + /* 将链表转化为 Array 并返回 */ toArray(): number[] { let node = this.stackPeek; const res = new Array(this.size); From 8060eb89da73ca633240a5606fb9881f544115dc Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Sat, 24 Dec 2022 09:01:22 +0800 Subject: [PATCH 5/7] feat: add linkedlist_stack in js to doc --- docs/chapter_stack_and_queue/stack.md | 58 ++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index c8149021a3..f87abe2ce2 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -390,7 +390,63 @@ comments: true === "JavaScript" ```js title="linkedlist_stack.js" - + /* 基于链表实现的栈 */ + class LinkedListStack { + #stackPeek; // 将头结点作为栈顶 + #stkSize = 0; // 栈的长度 + + constructor() { + this.#stackPeek = null; + } + + /* 获取栈的长度 */ + get size() { + return this.#stkSize; + } + + /* 判断栈是否为空 */ + isEmpty() { + return this.size == 0; + } + + /* 入栈 */ + push(num) { + const node = new ListNode(num); + node.next = this.#stackPeek; + this.#stackPeek = node; + this.#stkSize++; + } + + /* 出栈 */ + pop() { + const num = this.peek(); + if (!this.#stackPeek) { + throw new Error("栈为空!"); + } + this.#stackPeek = this.#stackPeek.next; + this.#stkSize--; + return num; + } + + /* 访问栈顶元素 */ + peek() { + if (!this.#stackPeek) { + throw new Error("栈为空!"); + } + return this.#stackPeek.val; + } + + /* 将链表转化为 Array 并返回 */ + toArray() { + let node = this.#stackPeek; + const res = new Array(this.size); + for (let i = res.length - 1; i >= 0; i--) { + res[i] = node.val; + node = node.next; + } + return res; + } + } ``` === "TypeScript" From 49fe239cfa96495a1b52b846951c4261dd8a1e14 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Sat, 24 Dec 2022 09:07:08 +0800 Subject: [PATCH 6/7] fix: remove redundancy --- codes/javascript/chapter_stack_and_queue/linkedlist_stack.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js index 15707a8d38..951616d2e5 100644 --- a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js @@ -1,4 +1,4 @@ -/**#stkSize +/** * File: linkedlist_stack.js * Created Time: 2022-12-22 * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) From 17909162fc922e5acab65a5cd9ef2aa818cf508f Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Sat, 24 Dec 2022 12:53:16 +0800 Subject: [PATCH 7/7] fine tune --- .../chapter_stack_and_queue/linkedlist_stack.js | 8 ++------ .../chapter_stack_and_queue/linkedlist_stack.ts | 12 +++++------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js index 951616d2e5..c6e9f27ce5 100644 --- a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js @@ -8,7 +8,7 @@ const ListNode = require("../include/ListNode"); /* 基于链表实现的栈 */ class LinkedListStack { - #stackPeek; // 将头结点作为栈顶 + #stackPeek; // 将头结点作为栈顶 #stkSize = 0; // 栈的长度 constructor() { @@ -36,9 +36,6 @@ class LinkedListStack { /* 出栈 */ pop() { const num = this.peek(); - if (!this.#stackPeek) { - throw new Error("栈为空!"); - } this.#stackPeek = this.#stackPeek.next; this.#stkSize--; return num; @@ -46,9 +43,8 @@ class LinkedListStack { /* 访问栈顶元素 */ peek() { - if (!this.#stackPeek) { + if (!this.#stackPeek) throw new Error("栈为空!"); - } return this.#stackPeek.val; } diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts index cdc4878947..1df4b4997b 100644 --- a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts @@ -9,7 +9,7 @@ import ListNode from "../module/ListNode" /* 基于链表实现的栈 */ class LinkedListStack { private stackPeek: ListNode | null; // 将头结点作为栈顶 - private stkSize: number = 0; // 栈的长度 + private stkSize: number = 0; // 栈的长度 constructor() { this.stackPeek = null; @@ -36,9 +36,8 @@ class LinkedListStack { /* 出栈 */ pop(): number { const num = this.peek(); - if (!this.stackPeek) { - throw new Error("栈为空!"); - } + if (!this.stackPeek) + throw new Error("栈为空"); this.stackPeek = this.stackPeek.next; this.stkSize--; return num; @@ -46,9 +45,8 @@ class LinkedListStack { /* 访问栈顶元素 */ peek(): number { - if (!this.stackPeek) { - throw new Error("栈为空!"); - } + if (!this.stackPeek) + throw new Error("栈为空"); return this.stackPeek.val; }