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] 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 000000000..a6d2a5188 --- /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 317bfe66b..d738598d5 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"