From 32867e3df335238dbdb049a31b99a03528d93b87 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Thu, 8 Dec 2022 22:03:41 +0800 Subject: [PATCH 1/5] feat: add code --- .../chapter_stack_and_queue/array_stack.ts | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 codes/typescript/chapter_stack_and_queue/array_stack.ts diff --git a/codes/typescript/chapter_stack_and_queue/array_stack.ts b/codes/typescript/chapter_stack_and_queue/array_stack.ts new file mode 100644 index 0000000000..5a57b6cf78 --- /dev/null +++ b/codes/typescript/chapter_stack_and_queue/array_stack.ts @@ -0,0 +1,86 @@ +/** + * File: array_stack.ts + * Created Time: 2022-12-08 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + + +/* 基于数组实现的栈 */ +class ArrayStack { + private stack: number[]; + constructor() { + this.stack = []; + } + /* 获取栈的长度 */ + get size(): number { + return this.stack.length; + } + + /* 判断栈是否为空 */ + empty(): boolean { + return this.stack.length === 0; + } + + /* 入栈 */ + push(num: number): void { + this.stack.push(num); + } + + /* 出栈 */ + pop(): number | undefined { + return this.stack.pop(); + } + + /* 访问栈顶元素 */ + top(): number | undefined { + return this.stack[this.stack.length - 1]; + } + + /* 访问索引 index 处元素 */ + get(index: number): number | undefined { + return this.stack[index]; + } + + /* 返回 Array */ + toArray() { + return this.stack; + } +}; + + +/* Driver Code */ + +/* 初始化栈 */ +const stack = new ArrayStack(); + +/* 元素入栈 */ +stack.push(1); +stack.push(3); +stack.push(2); +stack.push(5); +stack.push(4); +console.log("栈 stack = "); +console.log(stack.toArray()); + +/* 访问栈顶元素 */ +const top = stack.top(); +console.log("栈顶元素 top = " + top); + +/* 访问索引 index 处元素 */ +const num = stack.get(3); +console.log("栈索引 3 处的元素为 num = " + num); + +/* 元素出栈 */ +const pop = stack.pop(); +console.log("出栈元素 pop = " + pop + ",出栈后 stack = "); +console.log(stack.toArray()); + +/* 获取栈的长度 */ +const size = stack.size; +console.log("栈的长度 size = " + size); + +/* 判断是否为空 */ +const empty = stack.empty(); +console.log("栈是否为空 = " + empty); + +export { }; \ No newline at end of file From 03e5807f94f26423a85e3335d8da592e1b2f01b2 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Fri, 9 Dec 2022 00:03:04 +0800 Subject: [PATCH 2/5] feat: complement doc --- docs/chapter_stack_and_queue/stack.md | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index eed703353d..9a7e41683c 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -403,7 +403,46 @@ comments: true === "TypeScript" ```typescript title="linkedlist_stack.ts" + class ArrayStack { + private stack: number[]; + constructor() { + this.stack = []; + } + /* 获取栈的长度 */ + get size(): number { + return this.stack.length; + } + /* 判断栈是否为空 */ + empty(): boolean { + return this.stack.length === 0; + } + + /* 入栈 */ + push(num: number): void { + this.stack.push(num); + } + + /* 出栈 */ + pop(): number | undefined { + return this.stack.pop(); + } + + /* 访问栈顶元素 */ + top(): number | undefined { + return this.stack[this.stack.length - 1]; + } + + /* 访问索引 index 处元素 */ + get(index: number): number | undefined { + return this.stack[index]; + } + + /* 返回 Array */ + toArray() { + return this.stack; + } + }; ``` === "C" From 4839be74cd8651e278bd2b6aaf962406be72b16c Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Fri, 9 Dec 2022 00:05:17 +0800 Subject: [PATCH 3/5] feat: add array_stack in js --- .../chapter_stack_and_queue/array_stack.js | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 codes/javascript/chapter_stack_and_queue/array_stack.js diff --git a/codes/javascript/chapter_stack_and_queue/array_stack.js b/codes/javascript/chapter_stack_and_queue/array_stack.js new file mode 100644 index 0000000000..de748cb8df --- /dev/null +++ b/codes/javascript/chapter_stack_and_queue/array_stack.js @@ -0,0 +1,84 @@ +/** + * File: array_stack.ts + * Created Time: 2022-12-09 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + + +/* 基于数组实现的栈 */ +class ArrayStack { + stack; + constructor() { + this.stack = []; + } + /* 获取栈的长度 */ + get size() { + return this.stack.length; + } + + /* 判断栈是否为空 */ + empty() { + return this.stack.length === 0; + } + + /* 入栈 */ + push(num) { + this.stack.push(num); + } + + /* 出栈 */ + pop() { + return this.stack.pop(); + } + + /* 访问栈顶元素 */ + top() { + return this.stack[this.stack.length - 1]; + } + + /* 访问索引 index 处元素 */ + get(index) { + return this.stack[index]; + } + + /* 返回 Array */ + toArray() { + return this.stack; + } +}; + + +/* Driver Code */ + +/* 初始化栈 */ +const stack = new ArrayStack(); + +/* 元素入栈 */ +stack.push(1); +stack.push(3); +stack.push(2); +stack.push(5); +stack.push(4); +console.log("栈 stack = "); +console.log(stack.toArray()); + +/* 访问栈顶元素 */ +const top = stack.top(); +console.log("栈顶元素 top = " + top); + +/* 访问索引 index 处元素 */ +const num = stack.get(3); +console.log("栈索引 3 处的元素为 num = " + num); + +/* 元素出栈 */ +const pop = stack.pop(); +console.log("出栈元素 pop = " + pop + ",出栈后 stack = "); +console.log(stack.toArray()); + +/* 获取栈的长度 */ +const size = stack.size; +console.log("栈的长度 size = " + size); + +/* 判断是否为空 */ +const empty = stack.empty(); +console.log("栈是否为空 = " + empty); From 7ce5ae1608484725b356584529d5b9e60989c99c Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Fri, 9 Dec 2022 00:05:58 +0800 Subject: [PATCH 4/5] feat: complement array_stack doc in js --- docs/chapter_stack_and_queue/stack.md | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 9a7e41683c..7eb79fbc6a 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -397,7 +397,47 @@ comments: true === "JavaScript" ```js title="linkedlist_stack.js" + /* 基于数组实现的栈 */ + class ArrayStack { + stack; + constructor() { + this.stack = []; + } + /* 获取栈的长度 */ + get size() { + return this.stack.length; + } + /* 判断栈是否为空 */ + empty() { + return this.stack.length === 0; + } + + /* 入栈 */ + push(num) { + this.stack.push(num); + } + + /* 出栈 */ + pop() { + return this.stack.pop(); + } + + /* 访问栈顶元素 */ + top() { + return this.stack[this.stack.length - 1]; + } + + /* 访问索引 index 处元素 */ + get(index) { + return this.stack[index]; + } + + /* 返回 Array */ + toArray() { + return this.stack; + } + }; ``` === "TypeScript" From 15120d307a5cdb999bebbfb04603bad032348a52 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Sat, 10 Dec 2022 00:00:07 +0800 Subject: [PATCH 5/5] fix: fix names and move content to correct place --- .../chapter_stack_and_queue/array_stack.js | 2 +- docs/chapter_stack_and_queue/stack.md | 163 +++++++++--------- 2 files changed, 83 insertions(+), 82 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/array_stack.js b/codes/javascript/chapter_stack_and_queue/array_stack.js index de748cb8df..a83d16f0e8 100644 --- a/codes/javascript/chapter_stack_and_queue/array_stack.js +++ b/codes/javascript/chapter_stack_and_queue/array_stack.js @@ -1,5 +1,5 @@ /** - * File: array_stack.ts + * File: array_stack.js * Created Time: 2022-12-09 * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) */ diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 7eb79fbc6a..d988bdfe7c 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -397,92 +397,13 @@ comments: true === "JavaScript" ```js title="linkedlist_stack.js" - /* 基于数组实现的栈 */ - class ArrayStack { - stack; - constructor() { - this.stack = []; - } - /* 获取栈的长度 */ - get size() { - return this.stack.length; - } - - /* 判断栈是否为空 */ - empty() { - return this.stack.length === 0; - } - - /* 入栈 */ - push(num) { - this.stack.push(num); - } - - /* 出栈 */ - pop() { - return this.stack.pop(); - } - - /* 访问栈顶元素 */ - top() { - return this.stack[this.stack.length - 1]; - } - - /* 访问索引 index 处元素 */ - get(index) { - return this.stack[index]; - } - - /* 返回 Array */ - toArray() { - return this.stack; - } - }; + ``` === "TypeScript" ```typescript title="linkedlist_stack.ts" - class ArrayStack { - private stack: number[]; - constructor() { - this.stack = []; - } - /* 获取栈的长度 */ - get size(): number { - return this.stack.length; - } - - /* 判断栈是否为空 */ - empty(): boolean { - return this.stack.length === 0; - } - - /* 入栈 */ - push(num: number): void { - this.stack.push(num); - } - - /* 出栈 */ - pop(): number | undefined { - return this.stack.pop(); - } - - /* 访问栈顶元素 */ - top(): number | undefined { - return this.stack[this.stack.length - 1]; - } - - /* 访问索引 index 处元素 */ - get(index: number): number | undefined { - return this.stack[index]; - } - - /* 返回 Array */ - toArray() { - return this.stack; - } - }; + ``` === "C" @@ -666,13 +587,93 @@ comments: true === "JavaScript" ```js title="array_stack.js" + /* 基于数组实现的栈 */ + class ArrayStack { + stack; + constructor() { + this.stack = []; + } + /* 获取栈的长度 */ + get size() { + return this.stack.length; + } + + /* 判断栈是否为空 */ + empty() { + return this.stack.length === 0; + } + + /* 入栈 */ + push(num) { + this.stack.push(num); + } + /* 出栈 */ + pop() { + return this.stack.pop(); + } + + /* 访问栈顶元素 */ + top() { + return this.stack[this.stack.length - 1]; + } + + /* 访问索引 index 处元素 */ + get(index) { + return this.stack[index]; + } + + /* 返回 Array */ + toArray() { + return this.stack; + } + }; ``` === "TypeScript" ```typescript title="array_stack.ts" + /* 基于数组实现的栈 */ + class ArrayStack { + private stack: number[]; + constructor() { + this.stack = []; + } + /* 获取栈的长度 */ + get size(): number { + return this.stack.length; + } + + /* 判断栈是否为空 */ + empty(): boolean { + return this.stack.length === 0; + } + + /* 入栈 */ + push(num: number): void { + this.stack.push(num); + } + /* 出栈 */ + pop(): number | undefined { + return this.stack.pop(); + } + + /* 访问栈顶元素 */ + top(): number | undefined { + return this.stack[this.stack.length - 1]; + } + + /* 访问索引 index 处元素 */ + get(index: number): number | undefined { + return this.stack[index]; + } + + /* 返回 Array */ + toArray() { + return this.stack; + } + }; ``` === "C"