mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
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:
@ -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();
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化双向队列 */
|
||||
// JavaScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
|
||||
const deque = [];
|
||||
|
||||
@ -19,106 +19,106 @@ class ListNode {
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
class LinkedListDeque {
|
||||
front; // 头结点 front
|
||||
rear; // 尾结点 rear
|
||||
len; // 双向队列的长度
|
||||
#front; // 头结点 front
|
||||
#rear; // 尾结点 rear
|
||||
#queSize; // 双向队列的长度
|
||||
|
||||
constructor() {
|
||||
this.front = null;
|
||||
this.rear = null;
|
||||
this.len = 0;
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* 队尾入队操作 */
|
||||
pushLast(val) {
|
||||
const node = new ListNode(val);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
if (this.len === 0) {
|
||||
this.front = node;
|
||||
this.rear = node;
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// 将 node 添加至链表尾部
|
||||
this.rear.next = node;
|
||||
node.prev = this.rear;
|
||||
this.rear = node; // 更新尾结点
|
||||
this.#rear.next = node;
|
||||
node.prev = this.#rear;
|
||||
this.#rear = node; // 更新尾结点
|
||||
}
|
||||
this.len++;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 队首入队操作 */
|
||||
pushFirst(val) {
|
||||
const node = new ListNode(val);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
if (this.len === 0) {
|
||||
this.front = node;
|
||||
this.rear = node;
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// 将 node 添加至链表头部
|
||||
this.front.prev = node;
|
||||
node.next = this.front;
|
||||
this.front = node; // 更新头结点
|
||||
this.#front.prev = node;
|
||||
node.next = this.#front;
|
||||
this.#front = node; // 更新头结点
|
||||
}
|
||||
this.len++;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 队尾出队操作 */
|
||||
pollLast() {
|
||||
if (this.len === 0) {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.rear.val; // 存储尾结点值
|
||||
const value = this.#rear.val; // 存储尾结点值
|
||||
// 删除尾结点
|
||||
let temp = this.rear.prev;
|
||||
let temp = this.#rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.rear.prev = null;
|
||||
this.#rear.prev = null;
|
||||
}
|
||||
this.rear = temp; // 更新尾结点
|
||||
this.len--;
|
||||
this.#rear = temp; // 更新尾结点
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* 队首出队操作 */
|
||||
pollFirst() {
|
||||
if (this.len === 0) {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.front.val; // 存储尾结点值
|
||||
const value = this.#front.val; // 存储尾结点值
|
||||
// 删除头结点
|
||||
let temp = this.front.next;
|
||||
let temp = this.#front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.front.next = null;
|
||||
this.#front.next = null;
|
||||
}
|
||||
this.front = temp; // 更新头结点
|
||||
this.len--;
|
||||
this.#front = temp; // 更新头结点
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
peekLast() {
|
||||
return this.len === 0 ? null : this.rear.val;
|
||||
return this.#queSize === 0 ? null : this.#rear.val;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
peekFirst() {
|
||||
return this.len === 0 ? null : this.front.val;
|
||||
return this.#queSize === 0 ? null : this.#front.val;
|
||||
}
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
size() {
|
||||
return this.len;
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
isEmpty() {
|
||||
return this.len === 0;
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 打印双向队列 */
|
||||
print() {
|
||||
const arr = [];
|
||||
let temp = this.front;
|
||||
let temp = this.#front;
|
||||
while (temp !== null) {
|
||||
arr.push(temp.val);
|
||||
temp = temp.next;
|
||||
@ -127,6 +127,7 @@ class LinkedListDeque {
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化双向队列 */
|
||||
const linkedListDeque = new LinkedListDeque();
|
||||
linkedListDeque.pushLast(3);
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require("../include/ListNode");
|
||||
const { ListNode } = require("../modules/ListNode");
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require("../include/ListNode");
|
||||
const { ListNode } = require("../modules/ListNode");
|
||||
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
@ -60,7 +60,7 @@ class LinkedListStack {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化栈 */
|
||||
const stack = new LinkedListStack();
|
||||
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化队列 */
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
@ -16,16 +15,21 @@ queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log("队列 queue =", queue);
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue[0];
|
||||
console.log("队首元素 peek =", peek);
|
||||
|
||||
/* 元素出队 */
|
||||
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
||||
const poll = queue.shift();
|
||||
console.log("出队元素 poll =", poll, ",出队后 queue = ", queue);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.length;
|
||||
console.log("队列长度 size =", size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.length === 0;
|
||||
const isEmpty = queue.length === 0;
|
||||
console.log("队列是否为空 = ", isEmpty);
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化栈 */
|
||||
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
|
||||
@ -16,21 +15,21 @@ stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log("栈 stack =", stack)
|
||||
console.log("栈 stack =", stack);
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
const peek = stack[stack.length - 1];
|
||||
console.log("栈顶元素 peek =", peek)
|
||||
console.log("栈顶元素 peek =", peek);
|
||||
|
||||
/* 元素出栈 */
|
||||
const pop = stack.pop();
|
||||
console.log("出栈元素 pop =", pop)
|
||||
console.log("出栈后 stack =", stack)
|
||||
console.log("出栈元素 pop =", pop);
|
||||
console.log("出栈后 stack =", stack);
|
||||
|
||||
/* 获取栈的长度 */
|
||||
const size = stack.length;
|
||||
console.log("栈的长度 size =", size)
|
||||
console.log("栈的长度 size =", size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
const is_empty = stack.length === 0;
|
||||
console.log("栈是否为空 =", is_empty)
|
||||
const isEmpty = stack.length === 0;
|
||||
console.log("栈是否为空 =", isEmpty);
|
||||
|
||||
Reference in New Issue
Block a user