mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Merge branch 'master' of
github.com:krahets/hello-algo
This commit is contained in:
		
							
								
								
									
										84
									
								
								codes/javascript/chapter_stack_and_queue/array_stack.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								codes/javascript/chapter_stack_and_queue/array_stack.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,84 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * File: array_stack.js
 | 
				
			||||||
 | 
					 * Created Time: 2022-12-09
 | 
				
			||||||
 | 
					 * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 基于数组实现的栈 */
 | 
				
			||||||
 | 
					class ArrayStack {
 | 
				
			||||||
 | 
					    stack;
 | 
				
			||||||
 | 
					    constructor() {
 | 
				
			||||||
 | 
					        this.stack = [];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    /* 获取栈的长度 */
 | 
				
			||||||
 | 
					    get size() {
 | 
				
			||||||
 | 
					        return this.stack.length;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 判断栈是否为空 */
 | 
				
			||||||
 | 
					    empty() {
 | 
				
			||||||
 | 
					        return this.stack.length === 0;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 入栈 */
 | 
				
			||||||
 | 
					    push(num) {
 | 
				
			||||||
 | 
					        this.stack.push(num);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 出栈 */
 | 
				
			||||||
 | 
					    pop() {
 | 
				
			||||||
 | 
					        return this.stack.pop();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 访问栈顶元素 */
 | 
				
			||||||
 | 
					    top() {
 | 
				
			||||||
 | 
					        return this.stack[this.stack.length - 1];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 访问索引 index 处元素 */
 | 
				
			||||||
 | 
					    get(index) {
 | 
				
			||||||
 | 
					        return this.stack[index];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 返回 Array */
 | 
				
			||||||
 | 
					    toArray() {
 | 
				
			||||||
 | 
					        return this.stack;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Driver Code */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 初始化栈 */
 | 
				
			||||||
 | 
					const stack = new ArrayStack();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 元素入栈 */
 | 
				
			||||||
 | 
					stack.push(1);
 | 
				
			||||||
 | 
					stack.push(3);
 | 
				
			||||||
 | 
					stack.push(2);
 | 
				
			||||||
 | 
					stack.push(5);
 | 
				
			||||||
 | 
					stack.push(4);
 | 
				
			||||||
 | 
					console.log("栈 stack = ");
 | 
				
			||||||
 | 
					console.log(stack.toArray());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 访问栈顶元素 */
 | 
				
			||||||
 | 
					const top = stack.top();
 | 
				
			||||||
 | 
					console.log("栈顶元素 top = " + top);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 访问索引 index 处元素 */
 | 
				
			||||||
 | 
					const num = stack.get(3);
 | 
				
			||||||
 | 
					console.log("栈索引 3 处的元素为 num = " + num);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 元素出栈 */
 | 
				
			||||||
 | 
					const pop = stack.pop();
 | 
				
			||||||
 | 
					console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
 | 
				
			||||||
 | 
					console.log(stack.toArray());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 获取栈的长度 */
 | 
				
			||||||
 | 
					const size = stack.size;
 | 
				
			||||||
 | 
					console.log("栈的长度 size = " + size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 判断是否为空 */
 | 
				
			||||||
 | 
					const empty = stack.empty();
 | 
				
			||||||
 | 
					console.log("栈是否为空 = " + empty);
 | 
				
			||||||
							
								
								
									
										86
									
								
								codes/typescript/chapter_stack_and_queue/array_stack.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								codes/typescript/chapter_stack_and_queue/array_stack.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,86 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * File: array_stack.ts
 | 
				
			||||||
 | 
					 * Created Time: 2022-12-08
 | 
				
			||||||
 | 
					 * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 基于数组实现的栈 */
 | 
				
			||||||
 | 
					class ArrayStack {
 | 
				
			||||||
 | 
					    private stack: number[];
 | 
				
			||||||
 | 
					    constructor() {
 | 
				
			||||||
 | 
					        this.stack = [];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    /* 获取栈的长度 */
 | 
				
			||||||
 | 
					    get size(): number {
 | 
				
			||||||
 | 
					        return this.stack.length;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 判断栈是否为空 */
 | 
				
			||||||
 | 
					    empty(): boolean {
 | 
				
			||||||
 | 
					        return this.stack.length === 0;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 入栈 */
 | 
				
			||||||
 | 
					    push(num: number): void {
 | 
				
			||||||
 | 
					        this.stack.push(num);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 出栈 */
 | 
				
			||||||
 | 
					    pop(): number | undefined {
 | 
				
			||||||
 | 
					        return this.stack.pop();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 访问栈顶元素 */
 | 
				
			||||||
 | 
					    top(): number | undefined {
 | 
				
			||||||
 | 
					        return this.stack[this.stack.length - 1];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 访问索引 index 处元素 */
 | 
				
			||||||
 | 
					    get(index: number): number | undefined {
 | 
				
			||||||
 | 
					        return this.stack[index];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 返回 Array */
 | 
				
			||||||
 | 
					    toArray() {
 | 
				
			||||||
 | 
					        return this.stack;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Driver Code */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 初始化栈 */
 | 
				
			||||||
 | 
					const stack = new ArrayStack();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 元素入栈 */
 | 
				
			||||||
 | 
					stack.push(1);
 | 
				
			||||||
 | 
					stack.push(3);
 | 
				
			||||||
 | 
					stack.push(2);
 | 
				
			||||||
 | 
					stack.push(5);
 | 
				
			||||||
 | 
					stack.push(4);
 | 
				
			||||||
 | 
					console.log("栈 stack = ");
 | 
				
			||||||
 | 
					console.log(stack.toArray());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 访问栈顶元素 */
 | 
				
			||||||
 | 
					const top = stack.top();
 | 
				
			||||||
 | 
					console.log("栈顶元素 top = " + top);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 访问索引 index 处元素 */
 | 
				
			||||||
 | 
					const num = stack.get(3);
 | 
				
			||||||
 | 
					console.log("栈索引 3 处的元素为 num = " + num);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 元素出栈 */
 | 
				
			||||||
 | 
					const pop = stack.pop();
 | 
				
			||||||
 | 
					console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
 | 
				
			||||||
 | 
					console.log(stack.toArray());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 获取栈的长度 */
 | 
				
			||||||
 | 
					const size = stack.size;
 | 
				
			||||||
 | 
					console.log("栈的长度 size = " + size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 判断是否为空 */
 | 
				
			||||||
 | 
					const empty = stack.empty();
 | 
				
			||||||
 | 
					console.log("栈是否为空 = " + empty);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export { };
 | 
				
			||||||
@ -587,13 +587,93 @@ comments: true
 | 
				
			|||||||
=== "JavaScript"
 | 
					=== "JavaScript"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```js title="array_stack.js"
 | 
					    ```js title="array_stack.js"
 | 
				
			||||||
 | 
					    /* 基于数组实现的栈 */
 | 
				
			||||||
 | 
					    class ArrayStack {
 | 
				
			||||||
 | 
					        stack;
 | 
				
			||||||
 | 
					        constructor() {
 | 
				
			||||||
 | 
					            this.stack = [];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        /* 获取栈的长度 */
 | 
				
			||||||
 | 
					        get size() {
 | 
				
			||||||
 | 
					            return this.stack.length;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 判断栈是否为空 */
 | 
				
			||||||
 | 
					        empty() {
 | 
				
			||||||
 | 
					            return this.stack.length === 0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 入栈 */
 | 
				
			||||||
 | 
					        push(num) {
 | 
				
			||||||
 | 
					            this.stack.push(num);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 出栈 */
 | 
				
			||||||
 | 
					        pop() {
 | 
				
			||||||
 | 
					            return this.stack.pop();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 访问栈顶元素 */
 | 
				
			||||||
 | 
					        top() {
 | 
				
			||||||
 | 
					            return this.stack[this.stack.length - 1];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 访问索引 index 处元素 */
 | 
				
			||||||
 | 
					        get(index) {
 | 
				
			||||||
 | 
					            return this.stack[index];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 返回 Array */
 | 
				
			||||||
 | 
					        toArray() {
 | 
				
			||||||
 | 
					            return this.stack;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "TypeScript"
 | 
					=== "TypeScript"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```typescript title="array_stack.ts"
 | 
					    ```typescript title="array_stack.ts"
 | 
				
			||||||
 | 
					    /* 基于数组实现的栈 */
 | 
				
			||||||
 | 
					    class ArrayStack {
 | 
				
			||||||
 | 
					        private stack: number[];
 | 
				
			||||||
 | 
					        constructor() {
 | 
				
			||||||
 | 
					            this.stack = [];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        /* 获取栈的长度 */
 | 
				
			||||||
 | 
					        get size(): number {
 | 
				
			||||||
 | 
					            return this.stack.length;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 判断栈是否为空 */
 | 
				
			||||||
 | 
					        empty(): boolean {
 | 
				
			||||||
 | 
					            return this.stack.length === 0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 入栈 */
 | 
				
			||||||
 | 
					        push(num: number): void {
 | 
				
			||||||
 | 
					            this.stack.push(num);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 出栈 */
 | 
				
			||||||
 | 
					        pop(): number | undefined {
 | 
				
			||||||
 | 
					            return this.stack.pop();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 访问栈顶元素 */
 | 
				
			||||||
 | 
					        top(): number | undefined {
 | 
				
			||||||
 | 
					            return this.stack[this.stack.length - 1];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 访问索引 index 处元素 */
 | 
				
			||||||
 | 
					        get(index: number): number | undefined {
 | 
				
			||||||
 | 
					            return this.stack[index];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 返回 Array */
 | 
				
			||||||
 | 
					        toArray() {
 | 
				
			||||||
 | 
					            return this.stack;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "C"
 | 
					=== "C"
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user