mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-15 02:40:58 +08:00
feat: Traditional Chinese version (#1163)
* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
This commit is contained in:
156
zh-hant/codes/javascript/chapter_stack_and_queue/array_deque.js
Normal file
156
zh-hant/codes/javascript/chapter_stack_and_queue/array_deque.js
Normal file
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* File: array_deque.js
|
||||
* Created Time: 2023-02-28
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* 基於環形陣列實現的雙向佇列 */
|
||||
class ArrayDeque {
|
||||
#nums; // 用於儲存雙向佇列元素的陣列
|
||||
#front; // 佇列首指標,指向佇列首元素
|
||||
#queSize; // 雙向佇列長度
|
||||
|
||||
/* 建構子 */
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
this.#front = 0;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* 獲取雙向佇列的容量 */
|
||||
capacity() {
|
||||
return this.#nums.length;
|
||||
}
|
||||
|
||||
/* 獲取雙向佇列的長度 */
|
||||
size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 判斷雙向佇列是否為空 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 計算環形陣列索引 */
|
||||
index(i) {
|
||||
// 透過取餘操作實現陣列首尾相連
|
||||
// 當 i 越過陣列尾部後,回到頭部
|
||||
// 當 i 越過陣列頭部後,回到尾部
|
||||
return (i + this.capacity()) % this.capacity();
|
||||
}
|
||||
|
||||
/* 佇列首入列 */
|
||||
pushFirst(num) {
|
||||
if (this.#queSize === this.capacity()) {
|
||||
console.log('雙向佇列已滿');
|
||||
return;
|
||||
}
|
||||
// 佇列首指標向左移動一位
|
||||
// 透過取餘操作實現 front 越過陣列頭部後回到尾部
|
||||
this.#front = this.index(this.#front - 1);
|
||||
// 將 num 新增至佇列首
|
||||
this.#nums[this.#front] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 佇列尾入列 */
|
||||
pushLast(num) {
|
||||
if (this.#queSize === this.capacity()) {
|
||||
console.log('雙向佇列已滿');
|
||||
return;
|
||||
}
|
||||
// 計算佇列尾指標,指向佇列尾索引 + 1
|
||||
const rear = this.index(this.#front + this.#queSize);
|
||||
// 將 num 新增至佇列尾
|
||||
this.#nums[rear] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 佇列首出列 */
|
||||
popFirst() {
|
||||
const num = this.peekFirst();
|
||||
// 佇列首指標向後移動一位
|
||||
this.#front = this.index(this.#front + 1);
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 佇列尾出列 */
|
||||
popLast() {
|
||||
const num = this.peekLast();
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
peekFirst() {
|
||||
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* 訪問佇列尾元素 */
|
||||
peekLast() {
|
||||
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
||||
// 計算尾元素索引
|
||||
const last = this.index(this.#front + this.#queSize - 1);
|
||||
return this.#nums[last];
|
||||
}
|
||||
|
||||
/* 返回陣列用於列印 */
|
||||
toArray() {
|
||||
// 僅轉換有效長度範圍內的串列元素
|
||||
const res = [];
|
||||
for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {
|
||||
res[i] = this.#nums[this.index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化雙向佇列 */
|
||||
const capacity = 5;
|
||||
const deque = new ArrayDeque(capacity);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
console.log('雙向佇列 deque = [' + deque.toArray() + ']');
|
||||
|
||||
/* 訪問元素 */
|
||||
const peekFirst = deque.peekFirst();
|
||||
console.log('佇列首元素 peekFirst = ' + peekFirst);
|
||||
const peekLast = deque.peekLast();
|
||||
console.log('佇列尾元素 peekLast = ' + peekLast);
|
||||
|
||||
/* 元素入列 */
|
||||
deque.pushLast(4);
|
||||
console.log('元素 4 佇列尾入列後 deque = [' + deque.toArray() + ']');
|
||||
deque.pushFirst(1);
|
||||
console.log('元素 1 佇列首入列後 deque = [' + deque.toArray() + ']');
|
||||
|
||||
/* 元素出列 */
|
||||
const popLast = deque.popLast();
|
||||
console.log(
|
||||
'佇列尾出列元素 = ' +
|
||||
popLast +
|
||||
',佇列尾出列後 deque = [' +
|
||||
deque.toArray() +
|
||||
']'
|
||||
);
|
||||
const popFirst = deque.popFirst();
|
||||
console.log(
|
||||
'佇列首出列元素 = ' +
|
||||
popFirst +
|
||||
',佇列首出列後 deque = [' +
|
||||
deque.toArray() +
|
||||
']'
|
||||
);
|
||||
|
||||
/* 獲取雙向佇列的長度 */
|
||||
const size = deque.size();
|
||||
console.log('雙向佇列長度 size = ' + size);
|
||||
|
||||
/* 判斷雙向佇列是否為空 */
|
||||
const isEmpty = deque.isEmpty();
|
||||
console.log('雙向佇列是否為空 = ' + isEmpty);
|
||||
106
zh-hant/codes/javascript/chapter_stack_and_queue/array_queue.js
Normal file
106
zh-hant/codes/javascript/chapter_stack_and_queue/array_queue.js
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* File: array_queue.js
|
||||
* Created Time: 2022-12-13
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* 基於環形陣列實現的佇列 */
|
||||
class ArrayQueue {
|
||||
#nums; // 用於儲存佇列元素的陣列
|
||||
#front = 0; // 佇列首指標,指向佇列首元素
|
||||
#queSize = 0; // 佇列長度
|
||||
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
}
|
||||
|
||||
/* 獲取佇列的容量 */
|
||||
get capacity() {
|
||||
return this.#nums.length;
|
||||
}
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 入列 */
|
||||
push(num) {
|
||||
if (this.size === this.capacity) {
|
||||
console.log('佇列已滿');
|
||||
return;
|
||||
}
|
||||
// 計算佇列尾指標,指向佇列尾索引 + 1
|
||||
// 透過取餘操作實現 rear 越過陣列尾部後回到頭部
|
||||
const rear = (this.#front + this.size) % this.capacity;
|
||||
// 將 num 新增至佇列尾
|
||||
this.#nums[rear] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 出列 */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// 佇列首指標向後移動一位,若越過尾部,則返回到陣列頭部
|
||||
this.#front = (this.#front + 1) % this.capacity;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
peek() {
|
||||
if (this.isEmpty()) throw new Error('佇列為空');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
// 僅轉換有效長度範圍內的串列元素
|
||||
const arr = new Array(this.size);
|
||||
for (let i = 0, j = this.#front; i < this.size; i++, j++) {
|
||||
arr[i] = this.#nums[j % this.capacity];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化佇列 */
|
||||
const capacity = 10;
|
||||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入列 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('佇列 queue =', queue.toArray());
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
const peek = queue.peek();
|
||||
console.log('佇列首元素 peek = ' + peek);
|
||||
|
||||
/* 元素出列 */
|
||||
const pop = queue.pop();
|
||||
console.log('出列元素 pop = ' + pop + ',出列後 queue =', queue.toArray());
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
const size = queue.size;
|
||||
console.log('佇列長度 size = ' + size);
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('佇列是否為空 = ' + isEmpty);
|
||||
|
||||
/* 測試環形陣列 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
console.log('第 ' + i + ' 輪入列 + 出列後 queue =', queue.toArray());
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* File: array_stack.js
|
||||
* Created Time: 2022-12-09
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* 基於陣列實現的堆疊 */
|
||||
class ArrayStack {
|
||||
#stack;
|
||||
constructor() {
|
||||
this.#stack = [];
|
||||
}
|
||||
|
||||
/* 獲取堆疊的長度 */
|
||||
get size() {
|
||||
return this.#stack.length;
|
||||
}
|
||||
|
||||
/* 判斷堆疊是否為空 */
|
||||
isEmpty() {
|
||||
return this.#stack.length === 0;
|
||||
}
|
||||
|
||||
/* 入堆疊 */
|
||||
push(num) {
|
||||
this.#stack.push(num);
|
||||
}
|
||||
|
||||
/* 出堆疊 */
|
||||
pop() {
|
||||
if (this.isEmpty()) throw new Error('堆疊為空');
|
||||
return this.#stack.pop();
|
||||
}
|
||||
|
||||
/* 訪問堆疊頂元素 */
|
||||
top() {
|
||||
if (this.isEmpty()) throw new Error('堆疊為空');
|
||||
return this.#stack[this.#stack.length - 1];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
return this.#stack;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化堆疊 */
|
||||
const stack = new ArrayStack();
|
||||
|
||||
/* 元素入堆疊 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('堆疊 stack = ');
|
||||
console.log(stack.toArray());
|
||||
|
||||
/* 訪問堆疊頂元素 */
|
||||
const top = stack.top();
|
||||
console.log('堆疊頂元素 top = ' + top);
|
||||
|
||||
/* 元素出堆疊 */
|
||||
const pop = stack.pop();
|
||||
console.log('出堆疊元素 pop = ' + pop + ',出堆疊後 stack = ');
|
||||
console.log(stack.toArray());
|
||||
|
||||
/* 獲取堆疊的長度 */
|
||||
const size = stack.size;
|
||||
console.log('堆疊的長度 size = ' + size);
|
||||
|
||||
/* 判斷是否為空 */
|
||||
const isEmpty = stack.isEmpty();
|
||||
console.log('堆疊是否為空 = ' + isEmpty);
|
||||
44
zh-hant/codes/javascript/chapter_stack_and_queue/deque.js
Normal file
44
zh-hant/codes/javascript/chapter_stack_and_queue/deque.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: deque.js
|
||||
* Created Time: 2023-01-17
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化雙向佇列 */
|
||||
// JavaScript 沒有內建的雙端佇列,只能把 Array 當作雙端佇列來使用
|
||||
const deque = [];
|
||||
|
||||
/* 元素入列 */
|
||||
deque.push(2);
|
||||
deque.push(5);
|
||||
deque.push(4);
|
||||
// 請注意,由於是陣列,unshift() 方法的時間複雜度為 O(n)
|
||||
deque.unshift(3);
|
||||
deque.unshift(1);
|
||||
console.log('雙向佇列 deque = ', deque);
|
||||
|
||||
/* 訪問元素 */
|
||||
const peekFirst = deque[0];
|
||||
console.log('佇列首元素 peekFirst = ' + peekFirst);
|
||||
const peekLast = deque[deque.length - 1];
|
||||
console.log('佇列尾元素 peekLast = ' + peekLast);
|
||||
|
||||
/* 元素出列 */
|
||||
// 請注意,由於是陣列,shift() 方法的時間複雜度為 O(n)
|
||||
const popFront = deque.shift();
|
||||
console.log(
|
||||
'佇列首出列元素 popFront = ' + popFront + ',佇列首出列後 deque = ' + deque
|
||||
);
|
||||
const popBack = deque.pop();
|
||||
console.log(
|
||||
'佇列尾出列元素 popBack = ' + popBack + ',佇列尾出列後 deque = ' + deque
|
||||
);
|
||||
|
||||
/* 獲取雙向佇列的長度 */
|
||||
const size = deque.length;
|
||||
console.log('雙向佇列長度 size = ' + size);
|
||||
|
||||
/* 判斷雙向佇列是否為空 */
|
||||
const isEmpty = size === 0;
|
||||
console.log('雙向佇列是否為空 = ' + isEmpty);
|
||||
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* File: linkedlist_deque.js
|
||||
* Created Time: 2023-02-04
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* 雙向鏈結串列節點 */
|
||||
class ListNode {
|
||||
prev; // 前驅節點引用 (指標)
|
||||
next; // 後繼節點引用 (指標)
|
||||
val; // 節點值
|
||||
|
||||
constructor(val) {
|
||||
this.val = val;
|
||||
this.next = null;
|
||||
this.prev = null;
|
||||
}
|
||||
}
|
||||
|
||||
/* 基於雙向鏈結串列實現的雙向佇列 */
|
||||
class LinkedListDeque {
|
||||
#front; // 頭節點 front
|
||||
#rear; // 尾節點 rear
|
||||
#queSize; // 雙向佇列的長度
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* 佇列尾入列操作 */
|
||||
pushLast(val) {
|
||||
const node = new ListNode(val);
|
||||
// 若鏈結串列為空,則令 front 和 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.#queSize++;
|
||||
}
|
||||
|
||||
/* 佇列首入列操作 */
|
||||
pushFirst(val) {
|
||||
const node = new ListNode(val);
|
||||
// 若鏈結串列為空,則令 front 和 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.#queSize++;
|
||||
}
|
||||
|
||||
/* 佇列尾出列操作 */
|
||||
popLast() {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.#rear.val; // 儲存尾節點值
|
||||
// 刪除尾節點
|
||||
let temp = this.#rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.#rear.prev = null;
|
||||
}
|
||||
this.#rear = temp; // 更新尾節點
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* 佇列首出列操作 */
|
||||
popFirst() {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.#front.val; // 儲存尾節點值
|
||||
// 刪除頭節點
|
||||
let temp = this.#front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.#front.next = null;
|
||||
}
|
||||
this.#front = temp; // 更新頭節點
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* 訪問佇列尾元素 */
|
||||
peekLast() {
|
||||
return this.#queSize === 0 ? null : this.#rear.val;
|
||||
}
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
peekFirst() {
|
||||
return this.#queSize === 0 ? null : this.#front.val;
|
||||
}
|
||||
|
||||
/* 獲取雙向佇列的長度 */
|
||||
size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 判斷雙向佇列是否為空 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 列印雙向佇列 */
|
||||
print() {
|
||||
const arr = [];
|
||||
let temp = this.#front;
|
||||
while (temp !== null) {
|
||||
arr.push(temp.val);
|
||||
temp = temp.next;
|
||||
}
|
||||
console.log('[' + arr.join(', ') + ']');
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化雙向佇列 */
|
||||
const linkedListDeque = new LinkedListDeque();
|
||||
linkedListDeque.pushLast(3);
|
||||
linkedListDeque.pushLast(2);
|
||||
linkedListDeque.pushLast(5);
|
||||
console.log('雙向佇列 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 訪問元素 */
|
||||
const peekFirst = linkedListDeque.peekFirst();
|
||||
console.log('佇列首元素 peekFirst = ' + peekFirst);
|
||||
const peekLast = linkedListDeque.peekLast();
|
||||
console.log('佇列尾元素 peekLast = ' + peekLast);
|
||||
|
||||
/* 元素入列 */
|
||||
linkedListDeque.pushLast(4);
|
||||
console.log('元素 4 佇列尾入列後 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
linkedListDeque.pushFirst(1);
|
||||
console.log('元素 1 佇列首入列後 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 元素出列 */
|
||||
const popLast = linkedListDeque.popLast();
|
||||
console.log('佇列尾出列元素 = ' + popLast + ',佇列尾出列後 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
const popFirst = linkedListDeque.popFirst();
|
||||
console.log('佇列首出列元素 = ' + popFirst + ',佇列首出列後 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 獲取雙向佇列的長度 */
|
||||
const size = linkedListDeque.size();
|
||||
console.log('雙向佇列長度 size = ' + size);
|
||||
|
||||
/* 判斷雙向佇列是否為空 */
|
||||
const isEmpty = linkedListDeque.isEmpty();
|
||||
console.log('雙向佇列是否為空 = ' + isEmpty);
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* File: linkedlist_queue.js
|
||||
* Created Time: 2022-12-20
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 基於鏈結串列實現的佇列 */
|
||||
class LinkedListQueue {
|
||||
#front; // 頭節點 #front
|
||||
#rear; // 尾節點 #rear
|
||||
#queSize = 0;
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
}
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* 入列 */
|
||||
push(num) {
|
||||
// 在尾節點後新增 num
|
||||
const node = new ListNode(num);
|
||||
// 如果佇列為空,則令頭、尾節點都指向該節點
|
||||
if (!this.#front) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
// 如果佇列不為空,則將該節點新增到尾節點後
|
||||
} else {
|
||||
this.#rear.next = node;
|
||||
this.#rear = node;
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 出列 */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// 刪除頭節點
|
||||
this.#front = this.#front.next;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
peek() {
|
||||
if (this.size === 0) throw new Error('佇列為空');
|
||||
return this.#front.val;
|
||||
}
|
||||
|
||||
/* 將鏈結串列轉化為 Array 並返回 */
|
||||
toArray() {
|
||||
let node = this.#front;
|
||||
const res = new Array(this.size);
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化佇列 */
|
||||
const queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入列 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('佇列 queue = ' + queue.toArray());
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
const peek = queue.peek();
|
||||
console.log('佇列首元素 peek = ' + peek);
|
||||
|
||||
/* 元素出列 */
|
||||
const pop = queue.pop();
|
||||
console.log('出列元素 pop = ' + pop + ',出列後 queue = ' + queue.toArray());
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
const size = queue.size;
|
||||
console.log('佇列長度 size = ' + size);
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('佇列是否為空 = ' + isEmpty);
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* File: linkedlist_stack.js
|
||||
* Created Time: 2022-12-22
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 基於鏈結串列實現的堆疊 */
|
||||
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();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化堆疊 */
|
||||
const stack = new LinkedListStack();
|
||||
|
||||
/* 元素入堆疊 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('堆疊 stack = ' + stack.toArray());
|
||||
|
||||
/* 訪問堆疊頂元素 */
|
||||
const peek = stack.peek();
|
||||
console.log('堆疊頂元素 peek = ' + peek);
|
||||
|
||||
/* 元素出堆疊 */
|
||||
const pop = stack.pop();
|
||||
console.log('出堆疊元素 pop = ' + pop + ',出堆疊後 stack = ' + stack.toArray());
|
||||
|
||||
/* 獲取堆疊的長度 */
|
||||
const size = stack.size;
|
||||
console.log('堆疊的長度 size = ' + size);
|
||||
|
||||
/* 判斷是否為空 */
|
||||
const isEmpty = stack.isEmpty();
|
||||
console.log('堆疊是否為空 = ' + isEmpty);
|
||||
35
zh-hant/codes/javascript/chapter_stack_and_queue/queue.js
Normal file
35
zh-hant/codes/javascript/chapter_stack_and_queue/queue.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: queue.js
|
||||
* Created Time: 2022-12-05
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化佇列 */
|
||||
// JavaScript 沒有內建的佇列,可以把 Array 當作佇列來使用
|
||||
const queue = [];
|
||||
|
||||
/* 元素入列 */
|
||||
queue.push(1);
|
||||
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 pop = queue.shift();
|
||||
console.log('出列元素 pop =', pop, ',出列後 queue = ', queue);
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
const size = queue.length;
|
||||
console.log('佇列長度 size =', size);
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
const isEmpty = queue.length === 0;
|
||||
console.log('佇列是否為空 = ', isEmpty);
|
||||
35
zh-hant/codes/javascript/chapter_stack_and_queue/stack.js
Normal file
35
zh-hant/codes/javascript/chapter_stack_and_queue/stack.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: stack.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化堆疊 */
|
||||
// JavaScript 沒有內建的堆疊類別,可以把 Array 當作堆疊來使用
|
||||
const stack = [];
|
||||
|
||||
/* 元素入堆疊 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('堆疊 stack =', stack);
|
||||
|
||||
/* 訪問堆疊頂元素 */
|
||||
const peek = stack[stack.length - 1];
|
||||
console.log('堆疊頂元素 peek =', peek);
|
||||
|
||||
/* 元素出堆疊 */
|
||||
const pop = stack.pop();
|
||||
console.log('出堆疊元素 pop =', pop);
|
||||
console.log('出堆疊後 stack =', stack);
|
||||
|
||||
/* 獲取堆疊的長度 */
|
||||
const size = stack.length;
|
||||
console.log('堆疊的長度 size =', size);
|
||||
|
||||
/* 判斷是否為空 */
|
||||
const isEmpty = stack.length === 0;
|
||||
console.log('堆疊是否為空 =', isEmpty);
|
||||
Reference in New Issue
Block a user