mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 10:06:29 +08:00
5f7385c8a3
* 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
159 lines
4.4 KiB
C
159 lines
4.4 KiB
C
/**
|
|
* File: array_deque.c
|
|
* Created Time: 2023-03-13
|
|
* Author: Gonglja (glj0@outlook.com)
|
|
*/
|
|
|
|
#include "../utils/common.h"
|
|
|
|
/* 基於環形陣列實現的雙向佇列 */
|
|
typedef struct {
|
|
int *nums; // 用於儲存佇列元素的陣列
|
|
int front; // 佇列首指標,指向佇列首元素
|
|
int queSize; // 尾指標,指向佇列尾 + 1
|
|
int queCapacity; // 佇列容量
|
|
} ArrayDeque;
|
|
|
|
/* 建構子 */
|
|
ArrayDeque *newArrayDeque(int capacity) {
|
|
ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));
|
|
// 初始化陣列
|
|
deque->queCapacity = capacity;
|
|
deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);
|
|
deque->front = deque->queSize = 0;
|
|
return deque;
|
|
}
|
|
|
|
/* 析構函式 */
|
|
void delArrayDeque(ArrayDeque *deque) {
|
|
free(deque->nums);
|
|
free(deque);
|
|
}
|
|
|
|
/* 獲取雙向佇列的容量 */
|
|
int capacity(ArrayDeque *deque) {
|
|
return deque->queCapacity;
|
|
}
|
|
|
|
/* 獲取雙向佇列的長度 */
|
|
int size(ArrayDeque *deque) {
|
|
return deque->queSize;
|
|
}
|
|
|
|
/* 判斷雙向佇列是否為空 */
|
|
bool empty(ArrayDeque *deque) {
|
|
return deque->queSize == 0;
|
|
}
|
|
|
|
/* 計算環形陣列索引 */
|
|
int dequeIndex(ArrayDeque *deque, int i) {
|
|
// 透過取餘操作實現陣列首尾相連
|
|
// 當 i 越過陣列尾部時,回到頭部
|
|
// 當 i 越過陣列頭部後,回到尾部
|
|
return ((i + capacity(deque)) % capacity(deque));
|
|
}
|
|
|
|
/* 佇列首入列 */
|
|
void pushFirst(ArrayDeque *deque, int num) {
|
|
if (deque->queSize == capacity(deque)) {
|
|
printf("雙向佇列已滿\r\n");
|
|
return;
|
|
}
|
|
// 佇列首指標向左移動一位
|
|
// 透過取餘操作實現 front 越過陣列頭部回到尾部
|
|
deque->front = dequeIndex(deque, deque->front - 1);
|
|
// 將 num 新增到佇列首
|
|
deque->nums[deque->front] = num;
|
|
deque->queSize++;
|
|
}
|
|
|
|
/* 佇列尾入列 */
|
|
void pushLast(ArrayDeque *deque, int num) {
|
|
if (deque->queSize == capacity(deque)) {
|
|
printf("雙向佇列已滿\r\n");
|
|
return;
|
|
}
|
|
// 計算佇列尾指標,指向佇列尾索引 + 1
|
|
int rear = dequeIndex(deque, deque->front + deque->queSize);
|
|
// 將 num 新增至佇列尾
|
|
deque->nums[rear] = num;
|
|
deque->queSize++;
|
|
}
|
|
|
|
/* 訪問佇列首元素 */
|
|
int peekFirst(ArrayDeque *deque) {
|
|
// 訪問異常:雙向佇列為空
|
|
assert(empty(deque) == 0);
|
|
return deque->nums[deque->front];
|
|
}
|
|
|
|
/* 訪問佇列尾元素 */
|
|
int peekLast(ArrayDeque *deque) {
|
|
// 訪問異常:雙向佇列為空
|
|
assert(empty(deque) == 0);
|
|
int last = dequeIndex(deque, deque->front + deque->queSize - 1);
|
|
return deque->nums[last];
|
|
}
|
|
|
|
/* 佇列首出列 */
|
|
int popFirst(ArrayDeque *deque) {
|
|
int num = peekFirst(deque);
|
|
// 佇列首指標向後移動一位
|
|
deque->front = dequeIndex(deque, deque->front + 1);
|
|
deque->queSize--;
|
|
return num;
|
|
}
|
|
|
|
/* 佇列尾出列 */
|
|
int popLast(ArrayDeque *deque) {
|
|
int num = peekLast(deque);
|
|
deque->queSize--;
|
|
return num;
|
|
}
|
|
|
|
/* Driver Code */
|
|
int main() {
|
|
/* 初始化佇列 */
|
|
int capacity = 10;
|
|
ArrayDeque *deque = newArrayDeque(capacity);
|
|
pushLast(deque, 3);
|
|
pushLast(deque, 2);
|
|
pushLast(deque, 5);
|
|
printf("雙向佇列 deque = ");
|
|
printArray(deque->nums, deque->queSize);
|
|
|
|
/* 訪問元素 */
|
|
int peekFirstNum = peekFirst(deque);
|
|
printf("佇列首元素 peekFirst = %d\r\n", peekFirstNum);
|
|
int peekLastNum = peekLast(deque);
|
|
printf("佇列尾元素 peekLast = %d\r\n", peekLastNum);
|
|
|
|
/* 元素入列 */
|
|
pushLast(deque, 4);
|
|
printf("元素 4 佇列尾入列後 deque = ");
|
|
printArray(deque->nums, deque->queSize);
|
|
pushFirst(deque, 1);
|
|
printf("元素 1 佇列首入列後 deque = ");
|
|
printArray(deque->nums, deque->queSize);
|
|
|
|
/* 元素出列 */
|
|
int popLastNum = popLast(deque);
|
|
printf("佇列尾出列元素 = %d ,佇列尾出列後 deque= ", popLastNum);
|
|
printArray(deque->nums, deque->queSize);
|
|
int popFirstNum = popFirst(deque);
|
|
printf("佇列首出列元素 = %d ,佇列首出列後 deque= ", popFirstNum);
|
|
printArray(deque->nums, deque->queSize);
|
|
|
|
/* 獲取佇列的長度 */
|
|
int dequeSize = size(deque);
|
|
printf("雙向佇列長度 size = %d\r\n", dequeSize);
|
|
|
|
/* 判斷佇列是否為空 */
|
|
bool isEmpty = empty(deque);
|
|
printf("佇列是否為空 = %s\r\n", isEmpty ? "true" : "false");
|
|
|
|
// 釋放記憶體
|
|
delArrayDeque(deque);
|
|
|
|
return 0;
|
|
}
|