Add build script for JS and TS codes.

This commit is contained in:
krahets
2023-02-08 19:45:06 +08:00
parent 22b7d65d20
commit 05f0054005
28 changed files with 227 additions and 2217 deletions

View File

@ -356,125 +356,13 @@ comments: true
=== "JavaScript"
```javascript title="linkedlist_stack.js"
/* 基于链表实现的栈 */
class LinkedListStack {
#stackPeek; // 将头结点作为栈顶
#stkSize = 0; // 栈的长度
constructor() {
this.#stackPeek = null;
}
/* 获取栈的长度 */
get size() {
return this.#stkSize;
}
/* 判断栈是否为空 */
isEmpty() {
return this.size == 0;
}
/* 入栈 */
push(num) {
const node = new ListNode(num);
node.next = this.#stackPeek;
this.#stackPeek = node;
this.#stkSize++;
}
/* 出栈 */
pop() {
const num = this.peek();
if (!this.#stackPeek) {
throw new Error("栈为空!");
}
this.#stackPeek = this.#stackPeek.next;
this.#stkSize--;
return num;
}
/* 访问栈顶元素 */
peek() {
if (!this.#stackPeek) {
throw new Error("栈为空!");
}
return this.#stackPeek.val;
}
/* 将链表转化为 Array 并返回 */
toArray() {
let node = this.#stackPeek;
const res = new Array(this.size);
for (let i = res.length - 1; i >= 0; i--) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
[class]{LinkedListStack}-[func]{}
```
=== "TypeScript"
```typescript title="linkedlist_stack.ts"
/* 基于链表实现的栈 */
class LinkedListStack {
private stackPeek: ListNode | null; // 将头结点作为栈顶
private stkSize: number = 0; // 栈的长度
constructor() {
this.stackPeek = null;
}
/* 获取栈的长度 */
get size(): number {
return this.stkSize;
}
/* 判断栈是否为空 */
isEmpty(): boolean {
return this.size == 0;
}
/* 入栈 */
push(num: number): void {
const node = new ListNode(num);
node.next = this.stackPeek;
this.stackPeek = node;
this.stkSize++;
}
/* 出栈 */
pop(): number {
const num = this.peek();
if (!this.stackPeek) {
throw new Error("栈为空!");
}
this.stackPeek = this.stackPeek.next;
this.stkSize--;
return num;
}
/* 访问栈顶元素 */
peek(): number {
if (!this.stackPeek) {
throw new Error("栈为空!");
}
return this.stackPeek.val;
}
/* 将链表转化为 Array 并返回 */
toArray(): number[] {
let node = this.stackPeek;
const res = new Array<number>(this.size);
for (let i = res.length - 1; i >= 0; i--) {
res[i] = node!.val;
node = node!.next;
}
return res;
}
}
[class]{LinkedListStack}-[func]{}
```
=== "C"
@ -672,73 +560,13 @@ comments: true
=== "JavaScript"
```javascript 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() {
if (this.empty())
throw new Error("栈为空");
return this.stack.pop();
}
/* 访问栈顶元素 */
top() {
if (this.empty())
throw new Error("栈为空");
return this.stack[this.stack.length - 1];
}
};
[class]{ArrayStack}-[func]{}
```
=== "TypeScript"
```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 {
if (this.empty())
throw new Error('栈为空');
return this.stack.pop();
}
/* 访问栈顶元素 */
top(): number | undefined {
if (this.empty())
throw new Error('栈为空');
return this.stack[this.stack.length - 1];
}
};
[class]{ArrayStack}-[func]{}
```
=== "C"