From cf628007c85fa45f69dc8970aa106d995d515ea3 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X <10195101536@stu.ecnu.edu.cn> Date: Sat, 3 Dec 2022 23:00:13 +0800 Subject: [PATCH 1/4] Update stack.md add javascript and typescript for stack --- docs/chapter_stack_and_queue/stack.md | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 80870a0..317bfe6 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -143,13 +143,55 @@ comments: true === "JavaScript" ```js title="stack.js" + /* 初始化栈 */ + // Javascript 没有内置的栈类,可以把 Array 当作栈来使用 + const stack = []; + /* 元素入栈 */ + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); + + /* 访问栈顶元素 */ + peek = stack[stack.length-1]; + + /* 元素出栈 */ + pop = stack.pop(); + + /* 获取栈的长度 */ + size = stack.length; + + /* 判断是否为空 */ + is_empty = stack.length === 0; ``` === "TypeScript" ```typescript title="stack.ts" + /* 初始化栈 */ + // Typescript 没有内置的栈类,可以把 Array 当作栈来使用 + const stack:number[] = []; + /* 元素入栈 */ + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); + + /* 访问栈顶元素 */ + peek = stack[stack.length-1]; + + /* 元素出栈 */ + pop = stack.pop(); + + /* 获取栈的长度 */ + size = stack.length; + + /* 判断是否为空 */ + is_empty = stack.length === 0; ``` === "C" From 75fa643113df0f08a4c8adaf00a7abbb5e9f4664 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Sun, 4 Dec 2022 10:14:51 +0800 Subject: [PATCH 2/4] feat: add code in js --- .../chapter_stack_and_queue/stack.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 codes/javascript/chapter_stack_and_queue/stack.js diff --git a/codes/javascript/chapter_stack_and_queue/stack.js b/codes/javascript/chapter_stack_and_queue/stack.js new file mode 100644 index 0000000..1b2e83b --- /dev/null +++ b/codes/javascript/chapter_stack_and_queue/stack.js @@ -0,0 +1,28 @@ +/** + * File: stack.js + * Created Time: 2022-12-04 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + +/* 初始化栈 */ +// Javascript 没有内置的栈类,可以把 Array 当作栈来使用 +const stack = []; + +/* 元素入栈 */ +stack.push(1); +stack.push(3); +stack.push(2); +stack.push(5); +stack.push(4); + +/* 访问栈顶元素 */ +peek = stack[stack.length - 1]; + +/* 元素出栈 */ +pop = stack.pop(); + +/* 获取栈的长度 */ +size = stack.length; + +/* 判断是否为空 */ +is_empty = stack.length === 0; From b4991254dfe6bb77a8b31dc21ad53552979b0cf2 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Sun, 4 Dec 2022 10:17:18 +0800 Subject: [PATCH 3/4] feat: add code in ts --- .../chapter_stack_and_queue/stack.ts | 30 +++++++++++++++++++ docs/chapter_stack_and_queue/stack.md | 10 +++---- 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 codes/typescript/chapter_stack_and_queue/stack.ts diff --git a/codes/typescript/chapter_stack_and_queue/stack.ts b/codes/typescript/chapter_stack_and_queue/stack.ts new file mode 100644 index 0000000..a6d2a51 --- /dev/null +++ b/codes/typescript/chapter_stack_and_queue/stack.ts @@ -0,0 +1,30 @@ +/** + * File: stack.ts + * Created Time: 2022-12-04 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + +/* 初始化栈 */ +// Typescript 没有内置的栈类,可以把 Array 当作栈来使用 +const stack: number[] = []; + +/* 元素入栈 */ +stack.push(1); +stack.push(3); +stack.push(2); +stack.push(5); +stack.push(4); + +/* 访问栈顶元素 */ +const peek = stack[stack.length - 1]; + +/* 元素出栈 */ +const pop = stack.pop(); + +/* 获取栈的长度 */ +const size = stack.length; + +/* 判断是否为空 */ +const is_empty = stack.length === 0; + +export { }; \ No newline at end of file diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 317bfe6..d738598 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -172,7 +172,7 @@ comments: true ```typescript title="stack.ts" /* 初始化栈 */ // Typescript 没有内置的栈类,可以把 Array 当作栈来使用 - const stack:number[] = []; + const stack: number[] = []; /* 元素入栈 */ stack.push(1); @@ -182,16 +182,16 @@ comments: true stack.push(4); /* 访问栈顶元素 */ - peek = stack[stack.length-1]; + const peek = stack[stack.length - 1]; /* 元素出栈 */ - pop = stack.pop(); + const pop = stack.pop(); /* 获取栈的长度 */ - size = stack.length; + const size = stack.length; /* 判断是否为空 */ - is_empty = stack.length === 0; + const is_empty = stack.length === 0; ``` === "C" From a841a6fe42b54e22789cce4deddb1eea4a601560 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Sun, 4 Dec 2022 10:22:18 +0800 Subject: [PATCH 4/4] fix: use const to declare variables in js --- codes/javascript/chapter_stack_and_queue/stack.js | 8 ++++---- docs/chapter_stack_and_queue/stack.md | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/stack.js b/codes/javascript/chapter_stack_and_queue/stack.js index 1b2e83b..9afbdc2 100644 --- a/codes/javascript/chapter_stack_and_queue/stack.js +++ b/codes/javascript/chapter_stack_and_queue/stack.js @@ -16,13 +16,13 @@ stack.push(5); stack.push(4); /* 访问栈顶元素 */ -peek = stack[stack.length - 1]; +const peek = stack[stack.length - 1]; /* 元素出栈 */ -pop = stack.pop(); +const pop = stack.pop(); /* 获取栈的长度 */ -size = stack.length; +const size = stack.length; /* 判断是否为空 */ -is_empty = stack.length === 0; +const is_empty = stack.length === 0; diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index d738598..eed7033 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -155,16 +155,16 @@ comments: true stack.push(4); /* 访问栈顶元素 */ - peek = stack[stack.length-1]; + const peek = stack[stack.length-1]; /* 元素出栈 */ - pop = stack.pop(); + const pop = stack.pop(); /* 获取栈的长度 */ - size = stack.length; + const size = stack.length; /* 判断是否为空 */ - is_empty = stack.length === 0; + const is_empty = stack.length === 0; ``` === "TypeScript"