mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-21 15:57:14 +00: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:
@@ -0,0 +1,3 @@
|
||||
add_executable(array array.c)
|
||||
add_executable(linked_list linked_list.c)
|
||||
add_executable(my_list my_list.c)
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* File: array.c
|
||||
* Created Time: 2022-12-20
|
||||
* Author: MolDuM (moldum@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 隨機訪問元素 */
|
||||
int randomAccess(int *nums, int size) {
|
||||
// 在區間 [0, size) 中隨機抽取一個數字
|
||||
int randomIndex = rand() % size;
|
||||
// 獲取並返回隨機元素
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 擴展陣列長度 */
|
||||
int *extend(int *nums, int size, int enlarge) {
|
||||
// 初始化一個擴展長度後的陣列
|
||||
int *res = (int *)malloc(sizeof(int) * (size + enlarge));
|
||||
// 將原陣列中的所有元素複製到新陣列
|
||||
for (int i = 0; i < size; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 初始化擴展後的空間
|
||||
for (int i = size; i < size + enlarge; i++) {
|
||||
res[i] = 0;
|
||||
}
|
||||
// 返回擴展後的新陣列
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 在陣列的索引 index 處插入元素 num */
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// 把索引 index 以及之後的所有元素向後移動一位
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 將 num 賦給 index 處的元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 刪除索引 index 處的元素 */
|
||||
// 注意:stdio.h 佔用了 remove 關鍵詞
|
||||
void removeItem(int *nums, int size, int index) {
|
||||
// 把索引 index 之後的所有元素向前移動一位
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 走訪陣列 */
|
||||
void traverse(int *nums, int size) {
|
||||
int count = 0;
|
||||
// 透過索引走訪陣列
|
||||
for (int i = 0; i < size; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* 在陣列中查詢指定元素 */
|
||||
int find(int *nums, int size, int target) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化陣列 */
|
||||
int size = 5;
|
||||
int arr[5];
|
||||
printf("陣列 arr = ");
|
||||
printArray(arr, size);
|
||||
|
||||
int nums[] = {1, 3, 2, 5, 4};
|
||||
printf("陣列 nums = ");
|
||||
printArray(nums, size);
|
||||
|
||||
/* 隨機訪問 */
|
||||
int randomNum = randomAccess(nums, size);
|
||||
printf("在 nums 中獲取隨機元素 %d", randomNum);
|
||||
|
||||
/* 長度擴展 */
|
||||
int enlarge = 3;
|
||||
int *res = extend(nums, size, enlarge);
|
||||
size += enlarge;
|
||||
printf("將陣列長度擴展至 8 ,得到 nums = ");
|
||||
printArray(res, size);
|
||||
|
||||
/* 插入元素 */
|
||||
insert(res, size, 6, 3);
|
||||
printf("在索引 3 處插入數字 6 ,得到 nums = ");
|
||||
printArray(res, size);
|
||||
|
||||
/* 刪除元素 */
|
||||
removeItem(res, size, 2);
|
||||
printf("刪除索引 2 處的元素,得到 nums = ");
|
||||
printArray(res, size);
|
||||
|
||||
/* 走訪陣列 */
|
||||
traverse(res, size);
|
||||
|
||||
/* 查詢元素 */
|
||||
int index = find(res, size, 3);
|
||||
printf("在 res 中查詢元素 3 ,得到索引 = %d\n", index);
|
||||
|
||||
/* 釋放記憶體 */
|
||||
free(res);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* File: linked_list.c
|
||||
* Created Time: 2023-01-12
|
||||
* Author: Zero (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 在鏈結串列的節點 n0 之後插入節點 P */
|
||||
void insert(ListNode *n0, ListNode *P) {
|
||||
ListNode *n1 = n0->next;
|
||||
P->next = n1;
|
||||
n0->next = P;
|
||||
}
|
||||
|
||||
/* 刪除鏈結串列的節點 n0 之後的首個節點 */
|
||||
// 注意:stdio.h 佔用了 remove 關鍵詞
|
||||
void removeItem(ListNode *n0) {
|
||||
if (!n0->next)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode *P = n0->next;
|
||||
ListNode *n1 = P->next;
|
||||
n0->next = n1;
|
||||
// 釋放記憶體
|
||||
free(P);
|
||||
}
|
||||
|
||||
/* 訪問鏈結串列中索引為 index 的節點 */
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == NULL)
|
||||
return NULL;
|
||||
head = head->next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 在鏈結串列中查詢值為 target 的首個節點 */
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head) {
|
||||
if (head->val == target)
|
||||
return index;
|
||||
head = head->next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化鏈結串列 */
|
||||
// 初始化各個節點
|
||||
ListNode *n0 = newListNode(1);
|
||||
ListNode *n1 = newListNode(3);
|
||||
ListNode *n2 = newListNode(2);
|
||||
ListNode *n3 = newListNode(5);
|
||||
ListNode *n4 = newListNode(4);
|
||||
// 構建節點之間的引用
|
||||
n0->next = n1;
|
||||
n1->next = n2;
|
||||
n2->next = n3;
|
||||
n3->next = n4;
|
||||
printf("初始化的鏈結串列為\r\n");
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 插入節點 */
|
||||
insert(n0, newListNode(0));
|
||||
printf("插入節點後的鏈結串列為\r\n");
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 刪除節點 */
|
||||
removeItem(n0);
|
||||
printf("刪除節點後的鏈結串列為\r\n");
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 訪問節點 */
|
||||
ListNode *node = access(n0, 3);
|
||||
printf("鏈結串列中索引 3 處的節點的值 = %d\r\n", node->val);
|
||||
|
||||
/* 查詢節點 */
|
||||
int index = find(n0, 2);
|
||||
printf("鏈結串列中值為 2 的節點的索引 = %d\r\n", index);
|
||||
|
||||
// 釋放記憶體
|
||||
freeMemoryLinkedList(n0);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* File: my_list.c
|
||||
* Created Time: 2023-01-12
|
||||
* Author: Zero (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 串列類別 */
|
||||
typedef struct {
|
||||
int *arr; // 陣列(儲存串列元素)
|
||||
int capacity; // 串列容量
|
||||
int size; // 串列大小
|
||||
int extendRatio; // 串列每次擴容的倍數
|
||||
} MyList;
|
||||
|
||||
void extendCapacity(MyList *nums);
|
||||
|
||||
/* 建構子 */
|
||||
MyList *newMyList() {
|
||||
MyList *nums = malloc(sizeof(MyList));
|
||||
nums->capacity = 10;
|
||||
nums->arr = malloc(sizeof(int) * nums->capacity);
|
||||
nums->size = 0;
|
||||
nums->extendRatio = 2;
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* 析構函式 */
|
||||
void delMyList(MyList *nums) {
|
||||
free(nums->arr);
|
||||
free(nums);
|
||||
}
|
||||
|
||||
/* 獲取串列長度 */
|
||||
int size(MyList *nums) {
|
||||
return nums->size;
|
||||
}
|
||||
|
||||
/* 獲取串列容量 */
|
||||
int capacity(MyList *nums) {
|
||||
return nums->capacity;
|
||||
}
|
||||
|
||||
/* 訪問元素 */
|
||||
int get(MyList *nums, int index) {
|
||||
assert(index >= 0 && index < nums->size);
|
||||
return nums->arr[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
void set(MyList *nums, int index, int num) {
|
||||
assert(index >= 0 && index < nums->size);
|
||||
nums->arr[index] = num;
|
||||
}
|
||||
|
||||
/* 在尾部新增元素 */
|
||||
void add(MyList *nums, int num) {
|
||||
if (size(nums) == capacity(nums)) {
|
||||
extendCapacity(nums); // 擴容
|
||||
}
|
||||
nums->arr[size(nums)] = num;
|
||||
nums->size++;
|
||||
}
|
||||
|
||||
/* 在中間插入元素 */
|
||||
void insert(MyList *nums, int index, int num) {
|
||||
assert(index >= 0 && index < size(nums));
|
||||
// 元素數量超出容量時,觸發擴容機制
|
||||
if (size(nums) == capacity(nums)) {
|
||||
extendCapacity(nums); // 擴容
|
||||
}
|
||||
for (int i = size(nums); i > index; --i) {
|
||||
nums->arr[i] = nums->arr[i - 1];
|
||||
}
|
||||
nums->arr[index] = num;
|
||||
nums->size++;
|
||||
}
|
||||
|
||||
/* 刪除元素 */
|
||||
// 注意:stdio.h 佔用了 remove 關鍵詞
|
||||
int removeItem(MyList *nums, int index) {
|
||||
assert(index >= 0 && index < size(nums));
|
||||
int num = nums->arr[index];
|
||||
for (int i = index; i < size(nums) - 1; i++) {
|
||||
nums->arr[i] = nums->arr[i + 1];
|
||||
}
|
||||
nums->size--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 串列擴容 */
|
||||
void extendCapacity(MyList *nums) {
|
||||
// 先分配空間
|
||||
int newCapacity = capacity(nums) * nums->extendRatio;
|
||||
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
||||
int *temp = nums->arr;
|
||||
|
||||
// 複製舊資料到新資料
|
||||
for (int i = 0; i < size(nums); i++)
|
||||
extend[i] = nums->arr[i];
|
||||
|
||||
// 釋放舊資料
|
||||
free(temp);
|
||||
|
||||
// 更新新資料
|
||||
nums->arr = extend;
|
||||
nums->capacity = newCapacity;
|
||||
}
|
||||
|
||||
/* 將串列轉換為 Array 用於列印 */
|
||||
int *toArray(MyList *nums) {
|
||||
return nums->arr;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化串列 */
|
||||
MyList *nums = newMyList();
|
||||
/* 在尾部新增元素 */
|
||||
add(nums, 1);
|
||||
add(nums, 3);
|
||||
add(nums, 2);
|
||||
add(nums, 5);
|
||||
add(nums, 4);
|
||||
printf("串列 nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
printf("容量 = %d ,長度 = %d\n", capacity(nums), size(nums));
|
||||
|
||||
/* 在中間插入元素 */
|
||||
insert(nums, 3, 6);
|
||||
printf("在索引 3 處插入數字 6 ,得到 nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
|
||||
/* 刪除元素 */
|
||||
removeItem(nums, 3);
|
||||
printf("刪除索引 3 處的元素,得到 nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
|
||||
/* 訪問元素 */
|
||||
int num = get(nums, 1);
|
||||
printf("訪問索引 1 處的元素,得到 num = %d\n", num);
|
||||
|
||||
/* 更新元素 */
|
||||
set(nums, 1, 0);
|
||||
printf("將索引 1 處的元素更新為 0 ,得到 nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
|
||||
/* 測試擴容機制 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// 在 i = 5 時,串列長度將超出串列容量,此時觸發擴容機制
|
||||
add(nums, i);
|
||||
}
|
||||
|
||||
printf("擴容後的串列 nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
printf("容量 = %d ,長度 = %d\n", capacity(nums), size(nums));
|
||||
|
||||
/* 釋放分配記憶體 */
|
||||
delMyList(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user