You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
512 B
29 lines
512 B
2 years ago
|
/**
|
||
|
* 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;
|