Update JavaScript and TypeScript codes for all chapters, rename JavaScript and TypeScript import folder to modules (#402)

* Update JavaScript and TypeScript codes

* Rename JavaScript and TypeScript import folder to modules
This commit is contained in:
Justin Tse
2023-03-03 01:34:53 +08:00
committed by GitHub
parent 7b41e6c2f0
commit e4a98bc9c5
61 changed files with 324 additions and 290 deletions

View File

@ -7,49 +7,48 @@
/* 基于数组实现的栈 */
class ArrayStack {
stack;
#stack;
constructor() {
this.stack = [];
this.#stack = [];
}
/* 获取栈的长度 */
get size() {
return this.stack.length;
return this.#stack.length;
}
/* 判断栈是否为空 */
empty() {
return this.stack.length === 0;
return this.#stack.length === 0;
}
/* 入栈 */
push(num) {
this.stack.push(num);
this.#stack.push(num);
}
/* 出栈 */
pop() {
if (this.empty())
throw new Error("栈为空");
return this.stack.pop();
return this.#stack.pop();
}
/* 访问栈顶元素 */
top() {
if (this.empty())
throw new Error("栈为空");
return this.stack[this.stack.length - 1];
return this.#stack[this.#stack.length - 1];
}
/* 返回 Array */
toArray() {
return this.stack;
return this.#stack;
}
};
/* Driver Code */
/* 初始化栈 */
const stack = new ArrayStack();