diff --git a/codes/c/chapter_greedy/CMakeLists.txt b/codes/c/chapter_greedy/CMakeLists.txt index 7959b7d7c..f332d75fa 100644 --- a/codes/c/chapter_greedy/CMakeLists.txt +++ b/codes/c/chapter_greedy/CMakeLists.txt @@ -1,4 +1,4 @@ add_executable(coin_change_greedy coin_change_greedy.c) add_executable(fractional_knapsack fractional_knapsack.c) -add_executable(max_capacity fractional_knapmax_capacitysack.c) +add_executable(max_capacity max_capacity.c) add_executable(max_product_cutting max_product_cutting.c) diff --git a/codes/c/chapter_hashing/array_hash_map.c b/codes/c/chapter_hashing/array_hash_map.c index d5cb87659..a8798b03e 100644 --- a/codes/c/chapter_hashing/array_hash_map.c +++ b/codes/c/chapter_hashing/array_hash_map.c @@ -26,16 +26,16 @@ struct mapSet { typedef struct mapSet mapSet; /* 基于数组简易实现的哈希表 */ -struct ArrayHashMap { +struct arrayHashMap { pair *buckets[HASH_MAP_DEFAULT_SIZE]; }; -typedef struct ArrayHashMap ArrayHashMap; +typedef struct arrayHashMap arrayHashMap; /* 哈希表初始化函数 */ -ArrayHashMap *newArrayHashMap() { - ArrayHashMap *arrayHashMap = malloc(sizeof(ArrayHashMap)); - return arrayHashMap; +arrayHashMap *newArrayHashMap() { + arrayHashMap *map = malloc(sizeof(arrayHashMap)); + return map; } /* 哈希函数 */ @@ -45,7 +45,7 @@ int hashFunc(int key) { } /* 查询操作 */ -const char *get(const ArrayHashMap *d, const int key) { +const char *get(const arrayHashMap *d, const int key) { int index = hashFunc(key); const pair *pair = d->buckets[index]; if (pair == NULL) @@ -54,7 +54,7 @@ const char *get(const ArrayHashMap *d, const int key) { } /* 添加操作 */ -void put(ArrayHashMap *d, const int key, const char *val) { +void put(arrayHashMap *d, const int key, const char *val) { pair *pair = malloc(sizeof(pair)); pair->key = key; pair->val = malloc(strlen(val) + 1); @@ -65,7 +65,7 @@ void put(ArrayHashMap *d, const int key, const char *val) { } /* 删除操作 */ -void removeItem(ArrayHashMap *d, const int key) { +void removeItem(arrayHashMap *d, const int key) { int index = hashFunc(key); free(d->buckets[index]->val); free(d->buckets[index]); @@ -73,7 +73,7 @@ void removeItem(ArrayHashMap *d, const int key) { } /* 获取所有键值对 */ -void pairSet(ArrayHashMap *d, mapSet *set) { +void pairSet(arrayHashMap *d, mapSet *set) { pair *entries; int i = 0, index = 0; int total = 0; @@ -100,7 +100,7 @@ void pairSet(ArrayHashMap *d, mapSet *set) { } /* 获取所有键 */ -void keySet(ArrayHashMap *d, mapSet *set) { +void keySet(arrayHashMap *d, mapSet *set) { int *keys; int i = 0, index = 0; int total = 0; @@ -125,7 +125,7 @@ void keySet(ArrayHashMap *d, mapSet *set) { } /* 获取所有值 */ -void valueSet(ArrayHashMap *d, mapSet *set) { +void valueSet(arrayHashMap *d, mapSet *set) { char **vals; int i = 0, index = 0; int total = 0; @@ -150,7 +150,7 @@ void valueSet(ArrayHashMap *d, mapSet *set) { } /* 打印哈希表 */ -void print(ArrayHashMap *d) { +void print(arrayHashMap *d) { int i; mapSet set; pairSet(d, &set); @@ -164,7 +164,7 @@ void print(ArrayHashMap *d) { /* Driver Code */ int main() { /* 初始化哈希表 */ - ArrayHashMap *map = newArrayHashMap(); + arrayHashMap *map = newArrayHashMap(); /* 添加操作 */ // 在哈希表中添加键值对 (key, value) diff --git a/codes/c/chapter_searching/CMakeLists.txt b/codes/c/chapter_searching/CMakeLists.txt index 5c0dddba0..7b2a152d5 100644 --- a/codes/c/chapter_searching/CMakeLists.txt +++ b/codes/c/chapter_searching/CMakeLists.txt @@ -1,6 +1,4 @@ add_executable(binary_search binary_search.c) add_executable(two_sum two_sum.c) -add_executable(hashing_search hashing_search.c) -add_executable(linear_search linear_search.c) add_executable(binary_search_edge binary_search_edge.c) -add_executable(binary_search_insertion binary_search_insertion.c) \ No newline at end of file +add_executable(binary_search_insertion binary_search_insertion.c) diff --git a/codes/c/chapter_searching/hashing_search.c b/codes/c/chapter_searching/hashing_search.c deleted file mode 100644 index 826649087..000000000 --- a/codes/c/chapter_searching/hashing_search.c +++ /dev/null @@ -1,147 +0,0 @@ -/** - * File: hashing_search.c - * Created Time: 2023-04-15 - * Author: Gonglja (glj0@outlook.com) - */ - -#include "../utils/common.h" - -/* 哈希表 */ -struct hashTable { - int key; - void *val; - UT_hash_handle hh; // 基于 uthash.h 实现 -}; - -typedef struct hashTable hashTable; - -/* 打印哈希表(当 hashTable 中 val 为 int 型使用这个) */ -void printHashTableVal(hashTable **ht) { - struct hashTable *s; - for (s = *ht; s != NULL; s = s->hh.next) { - printf("key: %d: val: %d\n", s->key, *(int *)(s->val)); - } -} - -/* 打印哈希表(当 hashTable 中 val 为 ListNode* 型使用这个) */ -void printHashTableLinklist(hashTable **ht) { - struct hashTable *s; - for (s = *ht; s != NULL; s = s->hh.next) { - printf("key: %d: val: %d\n", s->key, ((ListNode *)s->val)->val); - } -} - -/* 哈希表查找 */ -hashTable *find(hashTable *ht, int k) { - hashTable *s; - HASH_FIND_INT(ht, &k, s); - return s; -} - -/* 添加 k、v 到哈希表 ht 中,v 为 int 类型 */ -void addInt(hashTable **ht, int k, int v) { - hashTable *s; - HASH_FIND_INT(*ht, &k, s); - if (s == NULL) { - s = malloc(sizeof(*s)); - s->key = k; - HASH_ADD_INT(*ht, key, s); - } - int *tmp = malloc(sizeof(*s->val)); - *tmp = v; - s->val = tmp; -} - -/* 添加 k、v 到哈希表 ht 中,v 为 ListNode* 类型 */ -void addLinkNode(hashTable **ht, int k, ListNode *v) { - hashTable *s; - HASH_FIND_INT(*ht, &k, s); - if (s == NULL) { - s = malloc(sizeof(*s)); - s->key = k; - HASH_ADD_INT(*ht, key, s); - } - s->val = (void *)v; -} - -/* 删除 ht 中所有元素 */ -void deleteAll(hashTable **ht) { - hashTable *curr, *tmp; - HASH_ITER(hh, *ht, curr, tmp) { - HASH_DEL(*ht, curr); - free(curr); - } -} - -/* 通过 key 访问哈希表,返回 int 类型 */ -int accessInt(hashTable **h, int key) { - hashTable *s; - int ret = -1; - if (s = find(*h, key)) { - ret = *(int *)s->val; - } - return ret; -} - -/* 通过 key 访问哈希表,返回 ListNode* 类型 */ -ListNode *accessLinkNode(hashTable **h, int key) { - hashTable *s; - ListNode *res = NULL; - if (s = find(*h, key)) { - res = (ListNode *)s->val; - } - return res; -} - -/* 哈希查找(数组) */ -int hashingSearchArray(hashTable **ht, int target) { - // 哈希表的 key: 目标元素,value: 索引 - // 若哈希表中无此 key ,返回 -1 - if (find(*ht, target) == NULL) - return -1; - return accessInt(ht, target); -} - -/* 哈希查找(链表) */ -ListNode *hashingSearchLinkedList(hashTable **ht, int target) { - // 哈希表的 key: 目标节点值,value: 节点对象 - // 若哈希表中无此 key ,返回 nullptr - if (find(*ht, target) == NULL) - return NULL; - return accessLinkNode(ht, target); -} - -/* Driver Code */ -int main() { - int target = 3; - - /* 哈希查找(数组) */ - int nums[] = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8}; - // 初始化哈希表 - hashTable **ht = malloc(sizeof(*ht)); - *ht = NULL; - for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) { - addInt(ht, nums[i], i); // key: 元素,value: 索引 - } - int index = hashingSearchArray(ht, target); - printf("目标元素 3 的索引 = %d\r\n", index); - deleteAll(ht); - - /* 哈希查找(链表) */ - ListNode *head = arrToLinkedList(nums, sizeof(nums) / sizeof(nums[0])); - // 初始化哈希表 - hashTable **ht1 = malloc(sizeof(*ht)); - *ht1 = NULL; - - while (head != NULL) { - addLinkNode(ht1, head->val, head); // key: 节点值,value: 节点 - head = head->next; - } - - ListNode *node = hashingSearchLinkedList(ht1, target); - if (node) - printf("目标节点值 3 的对应节点对象为 %#p val: %d\r\n", node, node->val); - deleteAll(ht1); - - return 0; -} diff --git a/codes/c/chapter_searching/linear_search.c b/codes/c/chapter_searching/linear_search.c deleted file mode 100644 index 9c97cd104..000000000 --- a/codes/c/chapter_searching/linear_search.c +++ /dev/null @@ -1,53 +0,0 @@ -/** - * File: linear_search.c - * Created Time: 2023-03-19 - * Author: Guanngxu (446678850@qq.com) - */ - -#include "../utils/common.h" - -/* 线性查找(数组) */ -int linearSearchArray(int *nums, int len, int target) { - // 遍历数组 - for (int i = 0; i < len; i++) { - // 找到目标元素,返回其索引 - if (nums[i] == target) - return i; - } - // 未找到目标元素,返回 -1 - return -1; -} - -/* 线性查找(链表) */ -ListNode *linearSearchLinkedList(ListNode *head, int target) { - // 遍历链表 - while (head != NULL) { - // 找到目标节点,返回之 - if (head->val == target) - return head; - head = head->next; - } - // 未找到目标节点,返回 NULL - return NULL; -} - -/* Driver Code */ -int main() { - int target = 3; - - /* 在数组中执行线性查找 */ - int nums[10] = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8}; - int index = linearSearchArray(nums, 10, target); - printf("目标元素 3 的索引 = %d\n", index); - - /* 在链表中执行线性查找 */ - ListNode *head = arrToLinkedList(nums, 10); - ListNode *node = linearSearchLinkedList(head, target); - if (node == NULL) { - printf("目标节点值 3 的对应节点对象为 NULL\n"); - } else { - printf("目标节点值 3 的对应节点对象为 addr: %p val: %d\n", node, node->val); - } - - return 0; -} diff --git a/docs/chapter_preface/about_the_book.assets/hello_algo_mindmap.jpg b/docs/chapter_preface/about_the_book.assets/hello_algo_mindmap.jpg index 38837bffc..2c379e27c 100644 Binary files a/docs/chapter_preface/about_the_book.assets/hello_algo_mindmap.jpg and b/docs/chapter_preface/about_the_book.assets/hello_algo_mindmap.jpg differ