Fomrat the JS and TS codes with prettier.

This commit is contained in:
krahets
2023-04-17 21:58:11 +08:00
parent 9a98ff8a5e
commit c4ea4e39f3
68 changed files with 634 additions and 510 deletions

View File

@ -4,14 +4,13 @@
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* 基于数组实现的栈 */
class ArrayStack {
#stack;
constructor() {
this.#stack = [];
}
/* 获取栈的长度 */
get size() {
return this.#stack.length;
@ -29,15 +28,13 @@ class ArrayStack {
/* 出栈 */
pop() {
if (this.empty())
throw new Error("栈为空");
if (this.empty()) throw new Error('栈为空');
return this.#stack.pop();
}
/* 访问栈顶元素 */
top() {
if (this.empty())
throw new Error("栈为空");
if (this.empty()) throw new Error('栈为空');
return this.#stack[this.#stack.length - 1];
}
@ -45,8 +42,7 @@ class ArrayStack {
toArray() {
return this.#stack;
}
};
}
/* Driver Code */
/* 初始化栈 */
@ -58,22 +54,22 @@ stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
console.log("栈 stack = ");
console.log('栈 stack = ');
console.log(stack.toArray());
/* 访问栈顶元素 */
const top = stack.top();
console.log("栈顶元素 top = " + top);
console.log('栈顶元素 top = ' + top);
/* 元素出栈 */
const pop = stack.pop();
console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
console.log('出栈元素 pop = ' + pop + ',出栈后 stack = ');
console.log(stack.toArray());
/* 获取栈的长度 */
const size = stack.size;
console.log("栈的长度 size = " + size);
console.log('栈的长度 size = ' + size);
/* 判断是否为空 */
const empty = stack.empty();
console.log("栈是否为空 = " + empty);
console.log('栈是否为空 = ' + empty);