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] 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 80870a078..317bfe66b 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"