|
|
|
@ -403,7 +403,46 @@ comments: true
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="linkedlist_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"
|
|
|
|
|