mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 18:37:48 +08:00 
			
		
		
		
	 5f7385c8a3
			
		
	
	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
		
			
				
	
	
		
			260 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			260 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /**
 | |
|  * File: vector.h
 | |
|  * Created Time: 2023-07-13
 | |
|  * Author: Zuoxun (845242523@qq.com)、Gonglja (glj0@outlook.com)
 | |
|  */
 | |
| 
 | |
| #ifndef VECTOR_H
 | |
| #define VECTOR_H
 | |
| 
 | |
| #ifdef __cplusplus
 | |
| extern "C" {
 | |
| #endif
 | |
| 
 | |
| /* 定義向量型別 */
 | |
| typedef struct vector {
 | |
|     int size;     // 當前向量的大小
 | |
|     int capacity; // 當前向量的容量
 | |
|     int depth;    // 當前向量的深度
 | |
|     void **data;  // 指向資料的指標陣列
 | |
| } vector;
 | |
| 
 | |
| /* 構造向量 */
 | |
| vector *newVector() {
 | |
|     vector *v = malloc(sizeof(vector));
 | |
|     v->size = 0;
 | |
|     v->capacity = 4;
 | |
|     v->depth = 1;
 | |
|     v->data = malloc(v->capacity * sizeof(void *));
 | |
|     return v;
 | |
| }
 | |
| 
 | |
| /* 構造向量,指定大小、元素預設值 */
 | |
| vector *_newVector(int size, void *elem, int elemSize) {
 | |
|     vector *v = malloc(sizeof(vector));
 | |
|     v->size = size;
 | |
|     v->capacity = size;
 | |
|     v->depth = 1;
 | |
|     v->data = malloc(v->capacity * sizeof(void *));
 | |
|     for (int i = 0; i < size; i++) {
 | |
|         void *tmp = malloc(sizeof(char) * elemSize);
 | |
|         memcpy(tmp, elem, elemSize);
 | |
|         v->data[i] = tmp;
 | |
|     }
 | |
|     return v;
 | |
| }
 | |
| 
 | |
| /* 析構向量 */
 | |
| void delVector(vector *v) {
 | |
|     if (v) {
 | |
|         if (v->depth == 0) {
 | |
|             return;
 | |
|         } else if (v->depth == 1) {
 | |
|             for (int i = 0; i < v->size; i++) {
 | |
|                 free(v->data[i]);
 | |
|             }
 | |
|             free(v);
 | |
|         } else {
 | |
|             for (int i = 0; i < v->size; i++) {
 | |
|                 delVector(v->data[i]);
 | |
|             }
 | |
|             v->depth--;
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| /*  新增元素(複製方式)到向量尾部 */
 | |
| void vectorPushback(vector *v, void *elem, int elemSize) {
 | |
|     if (v->size == v->capacity) {
 | |
|         v->capacity *= 2;
 | |
|         v->data = realloc(v->data, v->capacity * sizeof(void *));
 | |
|     }
 | |
|     void *tmp = malloc(sizeof(char) * elemSize);
 | |
|     memcpy(tmp, elem, elemSize);
 | |
|     v->data[v->size++] = tmp;
 | |
| }
 | |
| 
 | |
| /* 從向量尾部彈出元素 */
 | |
| void vectorPopback(vector *v) {
 | |
|     if (v->size != 0) {
 | |
|         free(v->data[v->size - 1]);
 | |
|         v->size--;
 | |
|     }
 | |
| }
 | |
| 
 | |
| /* 清空向量 */
 | |
| void vectorClear(vector *v) {
 | |
|     delVector(v);
 | |
|     v->size = 0;
 | |
|     v->capacity = 4;
 | |
|     v->depth = 1;
 | |
|     v->data = malloc(v->capacity * sizeof(void *));
 | |
| }
 | |
| 
 | |
| /* 獲取向量的大小 */
 | |
| int vectorSize(vector *v) {
 | |
|     return v->size;
 | |
| }
 | |
| 
 | |
| /* 獲取向量的尾元素 */
 | |
| void *vectorBack(vector *v) {
 | |
|     int n = v->size;
 | |
|     return n > 0 ? v->data[n - 1] : NULL;
 | |
| }
 | |
| 
 | |
| /* 獲取向量的頭元素 */
 | |
| void *vectorFront(vector *v) {
 | |
|     return v->size > 0 ? v->data[0] : NULL;
 | |
| }
 | |
| 
 | |
| /* 獲取向量下標 pos 的元素 */
 | |
| void *vectorAt(vector *v, int pos) {
 | |
|     if (pos < 0 || pos >= v->size) {
 | |
|         printf("vectorAt: out of range\n");
 | |
|         return NULL;
 | |
|     }
 | |
|     return v->data[pos];
 | |
| }
 | |
| 
 | |
| /* 設定向量下標 pos 的元素 */
 | |
| void vectorSet(vector *v, int pos, void *elem, int elemSize) {
 | |
|     if (pos < 0 || pos >= v->size) {
 | |
|         printf("vectorSet: out of range\n");
 | |
|         return;
 | |
|     }
 | |
|     free(v->data[pos]);
 | |
|     void *tmp = malloc(sizeof(char) * elemSize);
 | |
|     memcpy(tmp, elem, elemSize);
 | |
|     v->data[pos] = tmp;
 | |
| }
 | |
| 
 | |
| /* 向量擴容 */
 | |
| void vectorExpand(vector *v) {
 | |
|     v->capacity *= 2;
 | |
|     v->data = realloc(v->data, v->capacity * sizeof(void *));
 | |
| }
 | |
| 
 | |
| /* 向量縮容 */
 | |
| void vectorShrink(vector *v) {
 | |
|     v->capacity /= 2;
 | |
|     v->data = realloc(v->data, v->capacity * sizeof(void *));
 | |
| }
 | |
| 
 | |
| /* 在向量下標 pos 處插入元素 */
 | |
| void vectorInsert(vector *v, int pos, void *elem, int elemSize) {
 | |
|     if (v->size == v->capacity) {
 | |
|         vectorExpand(v);
 | |
|     }
 | |
|     for (int j = v->size; j > pos; j--) {
 | |
|         v->data[j] = v->data[j - 1];
 | |
|     }
 | |
|     void *tmp = malloc(sizeof(char) * elemSize);
 | |
|     memcpy(tmp, elem, elemSize);
 | |
|     v->data[pos] = tmp;
 | |
|     v->size++;
 | |
| }
 | |
| 
 | |
| /* 刪除向量下標 pos 處的元素 */
 | |
| void vectorErase(vector *v, int pos) {
 | |
|     if (v->size != 0) {
 | |
|         free(v->data[pos]);
 | |
|         for (int j = pos; j < v->size - 1; j++) {
 | |
|             v->data[j] = v->data[j + 1];
 | |
|         }
 | |
|         v->size--;
 | |
|     }
 | |
| }
 | |
| 
 | |
| /* 向量交換元素 */
 | |
| void vectorSwap(vector *v, int i, int j) {
 | |
|     void *tmp = v->data[i];
 | |
|     v->data[i] = v->data[j];
 | |
|     v->data[j] = tmp;
 | |
| }
 | |
| 
 | |
| /* 向量是否為空 */
 | |
| bool vectorEmpty(vector *v) {
 | |
|     return v->size == 0;
 | |
| }
 | |
| 
 | |
| /* 向量是否已滿 */
 | |
| bool vectorFull(vector *v) {
 | |
|     return v->size == v->capacity;
 | |
| }
 | |
| 
 | |
| /* 向量是否相等 */
 | |
| bool vectorEqual(vector *v1, vector *v2) {
 | |
|     if (v1->size != v2->size) {
 | |
|         printf("size not equal\n");
 | |
|         return false;
 | |
|     }
 | |
|     for (int i = 0; i < v1->size; i++) {
 | |
|         void *a = v1->data[i];
 | |
|         void *b = v2->data[i];
 | |
|         if (memcmp(a, b, sizeof(a)) != 0) {
 | |
|             printf("data %d not equal\n", i);
 | |
|             return false;
 | |
|         }
 | |
|     }
 | |
|     return true;
 | |
| }
 | |
| 
 | |
| /* 對向量內部進行排序 */
 | |
| void vectorSort(vector *v, int (*cmp)(const void *, const void *)) {
 | |
|     qsort(v->data, v->size, sizeof(void *), cmp);
 | |
| }
 | |
| 
 | |
| /* 列印函式, 需傳遞一個列印變數的函式進來 */
 | |
| /* 當前僅支持列印深度為 1 的 vector */
 | |
| void printVector(vector *v, void (*printFunc)(vector *v, void *p)) {
 | |
|     if (v) {
 | |
|         if (v->depth == 0) {
 | |
|             return;
 | |
|         } else if (v->depth == 1) {
 | |
|             if(v->size == 0) {
 | |
|                 printf("\n");
 | |
|                 return;
 | |
|             }
 | |
|             for (int i = 0; i < v->size; i++) {
 | |
|                 if (i == 0) {
 | |
|                     printf("[");
 | |
|                 } else if (i == v->size - 1) {
 | |
|                     printFunc(v, v->data[i]);
 | |
|                     printf("]\r\n");
 | |
|                     break;
 | |
|                 }
 | |
|                 printFunc(v, v->data[i]);
 | |
|                 printf(",");
 | |
|             }
 | |
|         } else {
 | |
|             for (int i = 0; i < v->size; i++) {
 | |
|                 printVector(v->data[i], printFunc);
 | |
|             }
 | |
|             v->depth--;
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| /* 當前僅支持列印深度為 2 的 vector */
 | |
| void printVectorMatrix(vector *vv, void (*printFunc)(vector *v, void *p)) {
 | |
|     printf("[\n");
 | |
|     for (int i = 0; i < vv->size; i++) {
 | |
|         vector *v = (vector *)vv->data[i];
 | |
|         printf("  [");
 | |
|         for (int j = 0; j < v->size; j++) {
 | |
|             printFunc(v, v->data[j]);
 | |
|             if (j != v->size - 1)
 | |
|                 printf(",");
 | |
|         }
 | |
|         printf("],");
 | |
|         printf("\n");
 | |
|     }
 | |
|     printf("]\n");
 | |
| }
 | |
| 
 | |
| #ifdef __cplusplus
 | |
| }
 | |
| #endif
 | |
| 
 | |
| #endif // VECTOR_H
 |