mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:46:29 +08:00
Update stack.md
add javascript and typescript for stack
This commit is contained in:
parent
ebf9024136
commit
cf628007c8
1 changed files with 42 additions and 0 deletions
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue