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] 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;