Merge pull request #70 from S-N-O-R-L-A-X/patch-1

Update stack.md
This commit is contained in:
Yudong Jin 2022-12-04 13:07:09 +08:00 committed by GitHub
commit 32e86e6011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 0 deletions

View File

@ -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);
/* 访问栈顶元素 */
const peek = stack[stack.length - 1];
/* 元素出栈 */
const pop = stack.pop();
/* 获取栈的长度 */
const size = stack.length;
/* 判断是否为空 */
const is_empty = stack.length === 0;

View File

@ -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 { };

View File

@ -143,13 +143,55 @@ comments: true
=== "JavaScript" === "JavaScript"
```js title="stack.js" ```js title="stack.js"
/* 初始化栈 */
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
const stack = [];
/* 元素入栈 */
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;
``` ```
=== "TypeScript" === "TypeScript"
```typescript title="stack.ts" ```typescript title="stack.ts"
/* 初始化栈 */
// 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;
``` ```
=== "C" === "C"