diff --git a/codes/c/chapter_graph/graph_adjacency_list.c b/codes/c/chapter_graph/graph_adjacency_list.c index 98584544d..7a61ead0a 100644 --- a/codes/c/chapter_graph/graph_adjacency_list.c +++ b/codes/c/chapter_graph/graph_adjacency_list.c @@ -109,7 +109,10 @@ void removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) { /* 添加顶点 */ void addVertex(GraphAdjList *graph, Vertex *vet) { - assert(graph != NULL && graph->size < MAX_SIZE); + assert(graph != NULL); + if (findNode(graph, vet) != NULL) + return; + assert(graph->size < MAX_SIZE); AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode)); head->vertex = vet; head->next = NULL; @@ -121,27 +124,6 @@ void addVertex(GraphAdjList *graph, Vertex *vet) { void removeVertex(GraphAdjList *graph, Vertex *vet) { AdjListNode *node = findNode(graph, vet); assert(node != NULL); - // 在邻接表中删除顶点 vet 对应的链表 - AdjListNode *cur = node, *pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - free(pre); - } - // 遍历其他顶点的链表,删除所有包含 vet 的边 - for (int i = 0; i < graph->size; i++) { - cur = graph->heads[i]; - pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - if (cur && cur->vertex == vet) { - pre->next = cur->next; - free(cur); - break; - } - } - } // 将该顶点之后的顶点向前移动,以填补空缺 int i; for (i = 0; i < graph->size; i++) { @@ -152,6 +134,25 @@ void removeVertex(GraphAdjList *graph, Vertex *vet) { graph->heads[j] = graph->heads[j + 1]; } graph->size--; + // 遍历其他顶点的链表,删除所有包含 vet 的边 + for (int i = 0; i < graph->size; i++) { + AdjListNode *cur = graph->heads[i]; + while (cur->next != NULL) { + AdjListNode *next = cur->next; + if (next->vertex == vet) { + cur->next = next->next; + free(next); + } else { + cur = next; + } + } + } + // 在邻接表中删除顶点 vet 对应的链表 + while (node != NULL) { + AdjListNode *next = node->next; + free(node); + node = next; + } free(vet); } diff --git a/codes/c/chapter_hashing/array_hash_map.c b/codes/c/chapter_hashing/array_hash_map.c index 17044934f..e166304a3 100644 --- a/codes/c/chapter_hashing/array_hash_map.c +++ b/codes/c/chapter_hashing/array_hash_map.c @@ -63,13 +63,13 @@ const char *get(const ArrayHashMap *hmap, const int key) { /* 添加操作 */ void put(ArrayHashMap *hmap, const int key, const char *val) { - Pair *Pair = malloc(sizeof(Pair)); - Pair->key = key; - Pair->val = malloc(strlen(val) + 1); - strcpy(Pair->val, val); + Pair *pair = malloc(sizeof(Pair)); + pair->key = key; + pair->val = malloc(strlen(val) + 1); + strcpy(pair->val, val); int index = hashFunc(key); - hmap->buckets[index] = Pair; + hmap->buckets[index] = pair; } /* 删除操作 */ diff --git a/codes/c/chapter_hashing/hash_map_open_addressing.c b/codes/c/chapter_hashing/hash_map_open_addressing.c index e4e00fb08..8641b53e3 100644 --- a/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -114,7 +114,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // 若找到键值对,则覆盖 val 并返回 if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) { free(hashMap->buckets[index]->val); - hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1)); + hashMap->buckets[index]->val = (char *)malloc(strlen(val) + 1); strcpy(hashMap->buckets[index]->val, val); hashMap->buckets[index]->val[strlen(val)] = '\0'; return; @@ -122,7 +122,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // 若键值对不存在,则添加该键值对 Pair *pair = (Pair *)malloc(sizeof(Pair)); pair->key = key; - pair->val = (char *)malloc(sizeof(strlen(val) + 1)); + pair->val = (char *)malloc(strlen(val) + 1); strcpy(pair->val, val); pair->val[strlen(val)] = '\0'; diff --git a/codes/c/chapter_sorting/counting_sort.c b/codes/c/chapter_sorting/counting_sort.c index aa77d1c82..a7512ce2a 100644 --- a/codes/c/chapter_sorting/counting_sort.c +++ b/codes/c/chapter_sorting/counting_sort.c @@ -45,7 +45,7 @@ void countingSort(int nums[], int size) { } // 2. 统计各数字的出现次数 // counter[num] 代表 num 的出现次数 - int *counter = calloc(m, sizeof(int)); + int *counter = calloc(m + 1, sizeof(int)); for (int i = 0; i < size; i++) { counter[nums[i]]++; } diff --git a/en/codes/c/chapter_graph/graph_adjacency_list.c b/en/codes/c/chapter_graph/graph_adjacency_list.c index cb8534099..7b06f7696 100644 --- a/en/codes/c/chapter_graph/graph_adjacency_list.c +++ b/en/codes/c/chapter_graph/graph_adjacency_list.c @@ -109,7 +109,10 @@ void removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) { /* Add a vertex */ void addVertex(GraphAdjList *graph, Vertex *vet) { - assert(graph != NULL && graph->size < MAX_SIZE); + assert(graph != NULL); + if (findNode(graph, vet) != NULL) + return; + assert(graph->size < MAX_SIZE); AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode)); head->vertex = vet; head->next = NULL; @@ -121,27 +124,6 @@ void addVertex(GraphAdjList *graph, Vertex *vet) { void removeVertex(GraphAdjList *graph, Vertex *vet) { AdjListNode *node = findNode(graph, vet); assert(node != NULL); - // Remove the list corresponding to vertex vet from the adjacency list - AdjListNode *cur = node, *pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - free(pre); - } - // Traverse the lists of the other vertices and remove all edges incident to vet - for (int i = 0; i < graph->size; i++) { - cur = graph->heads[i]; - pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - if (cur && cur->vertex == vet) { - pre->next = cur->next; - free(cur); - break; - } - } - } // Shift subsequent vertices forward to fill the gap int i; for (i = 0; i < graph->size; i++) { @@ -152,6 +134,25 @@ void removeVertex(GraphAdjList *graph, Vertex *vet) { graph->heads[j] = graph->heads[j + 1]; } graph->size--; + // Traverse the lists of the other vertices and remove all edges incident to vet + for (int i = 0; i < graph->size; i++) { + AdjListNode *cur = graph->heads[i]; + while (cur->next != NULL) { + AdjListNode *next = cur->next; + if (next->vertex == vet) { + cur->next = next->next; + free(next); + } else { + cur = next; + } + } + } + // Remove the list corresponding to vertex vet from the adjacency list + while (node != NULL) { + AdjListNode *next = node->next; + free(node); + node = next; + } free(vet); } diff --git a/en/codes/c/chapter_hashing/array_hash_map.c b/en/codes/c/chapter_hashing/array_hash_map.c index 4f570b4b4..ef09c29b5 100644 --- a/en/codes/c/chapter_hashing/array_hash_map.c +++ b/en/codes/c/chapter_hashing/array_hash_map.c @@ -63,13 +63,13 @@ const char *get(const ArrayHashMap *hmap, const int key) { /* Insert operation */ void put(ArrayHashMap *hmap, const int key, const char *val) { - Pair *Pair = malloc(sizeof(Pair)); - Pair->key = key; - Pair->val = malloc(strlen(val) + 1); - strcpy(Pair->val, val); + Pair *pair = malloc(sizeof(Pair)); + pair->key = key; + pair->val = malloc(strlen(val) + 1); + strcpy(pair->val, val); int index = hashFunc(key); - hmap->buckets[index] = Pair; + hmap->buckets[index] = pair; } /* Remove operation */ diff --git a/en/codes/c/chapter_hashing/hash_map_open_addressing.c b/en/codes/c/chapter_hashing/hash_map_open_addressing.c index b6af8d61b..0de47e66b 100644 --- a/en/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/en/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -114,7 +114,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // If the key-value pair is found, overwrite val and return if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) { free(hashMap->buckets[index]->val); - hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1)); + hashMap->buckets[index]->val = (char *)malloc(strlen(val) + 1); strcpy(hashMap->buckets[index]->val, val); hashMap->buckets[index]->val[strlen(val)] = '\0'; return; @@ -122,7 +122,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // If the key-value pair is absent, insert it Pair *pair = (Pair *)malloc(sizeof(Pair)); pair->key = key; - pair->val = (char *)malloc(sizeof(strlen(val) + 1)); + pair->val = (char *)malloc(strlen(val) + 1); strcpy(pair->val, val); pair->val[strlen(val)] = '\0'; diff --git a/en/codes/c/chapter_sorting/counting_sort.c b/en/codes/c/chapter_sorting/counting_sort.c index 6c17d06f0..136df5a9c 100644 --- a/en/codes/c/chapter_sorting/counting_sort.c +++ b/en/codes/c/chapter_sorting/counting_sort.c @@ -45,7 +45,7 @@ void countingSort(int nums[], int size) { } // 2. Count the occurrences of each number // counter[num] is the number of occurrences of num - int *counter = calloc(m, sizeof(int)); + int *counter = calloc(m + 1, sizeof(int)); for (int i = 0; i < size; i++) { counter[nums[i]]++; } diff --git a/ja/codes/c/chapter_graph/graph_adjacency_list.c b/ja/codes/c/chapter_graph/graph_adjacency_list.c index b703f346a..bbab99015 100644 --- a/ja/codes/c/chapter_graph/graph_adjacency_list.c +++ b/ja/codes/c/chapter_graph/graph_adjacency_list.c @@ -109,7 +109,10 @@ void removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) { /* 頂点を追加する */ void addVertex(GraphAdjList *graph, Vertex *vet) { - assert(graph != NULL && graph->size < MAX_SIZE); + assert(graph != NULL); + if (findNode(graph, vet) != NULL) + return; + assert(graph->size < MAX_SIZE); AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode)); head->vertex = vet; head->next = NULL; @@ -121,27 +124,6 @@ void addVertex(GraphAdjList *graph, Vertex *vet) { void removeVertex(GraphAdjList *graph, Vertex *vet) { AdjListNode *node = findNode(graph, vet); assert(node != NULL); - // 隣接リストから頂点 vet に対応する連結リストを削除する - AdjListNode *cur = node, *pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - free(pre); - } - // 他の頂点の連結リストを走査し、vet を含むすべての辺を削除する - for (int i = 0; i < graph->size; i++) { - cur = graph->heads[i]; - pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - if (cur && cur->vertex == vet) { - pre->next = cur->next; - free(cur); - break; - } - } - } // この頂点より後の頂点を前にずらし、空きを埋める int i; for (i = 0; i < graph->size; i++) { @@ -152,6 +134,25 @@ void removeVertex(GraphAdjList *graph, Vertex *vet) { graph->heads[j] = graph->heads[j + 1]; } graph->size--; + // 他の頂点の連結リストを走査し、vet を含むすべての辺を削除する + for (int i = 0; i < graph->size; i++) { + AdjListNode *cur = graph->heads[i]; + while (cur->next != NULL) { + AdjListNode *next = cur->next; + if (next->vertex == vet) { + cur->next = next->next; + free(next); + } else { + cur = next; + } + } + } + // 隣接リストから頂点 vet に対応する連結リストを削除する + while (node != NULL) { + AdjListNode *next = node->next; + free(node); + node = next; + } free(vet); } diff --git a/ja/codes/c/chapter_hashing/array_hash_map.c b/ja/codes/c/chapter_hashing/array_hash_map.c index e8db7cac4..848146172 100644 --- a/ja/codes/c/chapter_hashing/array_hash_map.c +++ b/ja/codes/c/chapter_hashing/array_hash_map.c @@ -63,13 +63,13 @@ const char *get(const ArrayHashMap *hmap, const int key) { /* 追加操作 */ void put(ArrayHashMap *hmap, const int key, const char *val) { - Pair *Pair = malloc(sizeof(Pair)); - Pair->key = key; - Pair->val = malloc(strlen(val) + 1); - strcpy(Pair->val, val); + Pair *pair = malloc(sizeof(Pair)); + pair->key = key; + pair->val = malloc(strlen(val) + 1); + strcpy(pair->val, val); int index = hashFunc(key); - hmap->buckets[index] = Pair; + hmap->buckets[index] = pair; } /* 削除操作 */ diff --git a/ja/codes/c/chapter_hashing/hash_map_open_addressing.c b/ja/codes/c/chapter_hashing/hash_map_open_addressing.c index f8a9d0c5b..6ae2512b8 100644 --- a/ja/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/ja/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -114,7 +114,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // キーと値のペアが見つかった場合、val を上書きして戻る if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) { free(hashMap->buckets[index]->val); - hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1)); + hashMap->buckets[index]->val = (char *)malloc(strlen(val) + 1); strcpy(hashMap->buckets[index]->val, val); hashMap->buckets[index]->val[strlen(val)] = '\0'; return; @@ -122,7 +122,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // キーと値のペアが存在しない場合、そのペアを追加する Pair *pair = (Pair *)malloc(sizeof(Pair)); pair->key = key; - pair->val = (char *)malloc(sizeof(strlen(val) + 1)); + pair->val = (char *)malloc(strlen(val) + 1); strcpy(pair->val, val); pair->val[strlen(val)] = '\0'; diff --git a/ja/codes/c/chapter_sorting/counting_sort.c b/ja/codes/c/chapter_sorting/counting_sort.c index c8ea2003d..149745e2f 100644 --- a/ja/codes/c/chapter_sorting/counting_sort.c +++ b/ja/codes/c/chapter_sorting/counting_sort.c @@ -45,7 +45,7 @@ void countingSort(int nums[], int size) { } // 2. 各数値の出現回数を数える // counter[num] は num の出現回数を表す - int *counter = calloc(m, sizeof(int)); + int *counter = calloc(m + 1, sizeof(int)); for (int i = 0; i < size; i++) { counter[nums[i]]++; } diff --git a/ko/codes/c/chapter_graph/graph_adjacency_list.c b/ko/codes/c/chapter_graph/graph_adjacency_list.c index 1eb33643a..524ce8340 100644 --- a/ko/codes/c/chapter_graph/graph_adjacency_list.c +++ b/ko/codes/c/chapter_graph/graph_adjacency_list.c @@ -109,7 +109,10 @@ void removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) { /* 정점 추가 */ void addVertex(GraphAdjList *graph, Vertex *vet) { - assert(graph != NULL && graph->size < MAX_SIZE); + assert(graph != NULL); + if (findNode(graph, vet) != NULL) + return; + assert(graph->size < MAX_SIZE); AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode)); head->vertex = vet; head->next = NULL; @@ -121,27 +124,6 @@ void addVertex(GraphAdjList *graph, Vertex *vet) { void removeVertex(GraphAdjList *graph, Vertex *vet) { AdjListNode *node = findNode(graph, vet); assert(node != NULL); - // 인접 리스트에서 정점 vet에 해당하는 연결 리스트를 삭제한다 - AdjListNode *cur = node, *pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - free(pre); - } - // 다른 정점의 연결 리스트를 순회하여 vet를 포함하는 모든 간선을 삭제한다 - for (int i = 0; i < graph->size; i++) { - cur = graph->heads[i]; - pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - if (cur && cur->vertex == vet) { - pre->next = cur->next; - free(cur); - break; - } - } - } // 해당 정점 뒤의 정점을 앞으로 옮겨 빈자리를 채운다 int i; for (i = 0; i < graph->size; i++) { @@ -152,6 +134,25 @@ void removeVertex(GraphAdjList *graph, Vertex *vet) { graph->heads[j] = graph->heads[j + 1]; } graph->size--; + // 다른 정점의 연결 리스트를 순회하여 vet를 포함하는 모든 간선을 삭제한다 + for (int i = 0; i < graph->size; i++) { + AdjListNode *cur = graph->heads[i]; + while (cur->next != NULL) { + AdjListNode *next = cur->next; + if (next->vertex == vet) { + cur->next = next->next; + free(next); + } else { + cur = next; + } + } + } + // 인접 리스트에서 정점 vet에 해당하는 연결 리스트를 삭제한다 + while (node != NULL) { + AdjListNode *next = node->next; + free(node); + node = next; + } free(vet); } diff --git a/ko/codes/c/chapter_hashing/array_hash_map.c b/ko/codes/c/chapter_hashing/array_hash_map.c index fb478f28e..8d013610f 100644 --- a/ko/codes/c/chapter_hashing/array_hash_map.c +++ b/ko/codes/c/chapter_hashing/array_hash_map.c @@ -63,13 +63,13 @@ const char *get(const ArrayHashMap *hmap, const int key) { /* 추가 연산 */ void put(ArrayHashMap *hmap, const int key, const char *val) { - Pair *Pair = malloc(sizeof(Pair)); - Pair->key = key; - Pair->val = malloc(strlen(val) + 1); - strcpy(Pair->val, val); + Pair *pair = malloc(sizeof(Pair)); + pair->key = key; + pair->val = malloc(strlen(val) + 1); + strcpy(pair->val, val); int index = hashFunc(key); - hmap->buckets[index] = Pair; + hmap->buckets[index] = pair; } /* 삭제 연산 */ diff --git a/ko/codes/c/chapter_hashing/hash_map_open_addressing.c b/ko/codes/c/chapter_hashing/hash_map_open_addressing.c index e6f3acd3c..c7a92a7fa 100644 --- a/ko/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/ko/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -114,7 +114,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // 키-값 쌍을 찾으면 val을 덮어쓰고 반환한다 if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) { free(hashMap->buckets[index]->val); - hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1)); + hashMap->buckets[index]->val = (char *)malloc(strlen(val) + 1); strcpy(hashMap->buckets[index]->val, val); hashMap->buckets[index]->val[strlen(val)] = '\0'; return; @@ -122,7 +122,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // 키-값 쌍이 없으면 해당 키-값 쌍을 추가한다 Pair *pair = (Pair *)malloc(sizeof(Pair)); pair->key = key; - pair->val = (char *)malloc(sizeof(strlen(val) + 1)); + pair->val = (char *)malloc(strlen(val) + 1); strcpy(pair->val, val); pair->val[strlen(val)] = '\0'; diff --git a/ko/codes/c/chapter_sorting/counting_sort.c b/ko/codes/c/chapter_sorting/counting_sort.c index e0a2179d7..4af4f641c 100644 --- a/ko/codes/c/chapter_sorting/counting_sort.c +++ b/ko/codes/c/chapter_sorting/counting_sort.c @@ -45,7 +45,7 @@ void countingSort(int nums[], int size) { } // 2. 각 숫자의 등장 횟수를 센다 // counter[num]은 num의 등장 횟수를 나타낸다 - int *counter = calloc(m, sizeof(int)); + int *counter = calloc(m + 1, sizeof(int)); for (int i = 0; i < size; i++) { counter[nums[i]]++; } diff --git a/ru/codes/c/chapter_graph/graph_adjacency_list.c b/ru/codes/c/chapter_graph/graph_adjacency_list.c index 2ee8281a8..f16bd1d7d 100644 --- a/ru/codes/c/chapter_graph/graph_adjacency_list.c +++ b/ru/codes/c/chapter_graph/graph_adjacency_list.c @@ -109,7 +109,10 @@ void removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) { /* Добавление вершины */ void addVertex(GraphAdjList *graph, Vertex *vet) { - assert(graph != NULL && graph->size < MAX_SIZE); + assert(graph != NULL); + if (findNode(graph, vet) != NULL) + return; + assert(graph->size < MAX_SIZE); AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode)); head->vertex = vet; head->next = NULL; @@ -121,27 +124,6 @@ void addVertex(GraphAdjList *graph, Vertex *vet) { void removeVertex(GraphAdjList *graph, Vertex *vet) { AdjListNode *node = findNode(graph, vet); assert(node != NULL); - // Удалить из списка смежности список, соответствующий вершине vet - AdjListNode *cur = node, *pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - free(pre); - } - // Обойти списки других вершин и удалить все ребра, содержащие vet - for (int i = 0; i < graph->size; i++) { - cur = graph->heads[i]; - pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - if (cur && cur->vertex == vet) { - pre->next = cur->next; - free(cur); - break; - } - } - } // Сдвинуть вершины после данной вперед, чтобы заполнить образовавшийся пробел int i; for (i = 0; i < graph->size; i++) { @@ -152,6 +134,25 @@ void removeVertex(GraphAdjList *graph, Vertex *vet) { graph->heads[j] = graph->heads[j + 1]; } graph->size--; + // Обойти списки других вершин и удалить все ребра, содержащие vet + for (int i = 0; i < graph->size; i++) { + AdjListNode *cur = graph->heads[i]; + while (cur->next != NULL) { + AdjListNode *next = cur->next; + if (next->vertex == vet) { + cur->next = next->next; + free(next); + } else { + cur = next; + } + } + } + // Удалить из списка смежности список, соответствующий вершине vet + while (node != NULL) { + AdjListNode *next = node->next; + free(node); + node = next; + } free(vet); } diff --git a/ru/codes/c/chapter_hashing/array_hash_map.c b/ru/codes/c/chapter_hashing/array_hash_map.c index cb2c2cdb3..254563cbe 100644 --- a/ru/codes/c/chapter_hashing/array_hash_map.c +++ b/ru/codes/c/chapter_hashing/array_hash_map.c @@ -63,13 +63,13 @@ const char *get(const ArrayHashMap *hmap, const int key) { /* Операция добавления */ void put(ArrayHashMap *hmap, const int key, const char *val) { - Pair *Pair = malloc(sizeof(Pair)); - Pair->key = key; - Pair->val = malloc(strlen(val) + 1); - strcpy(Pair->val, val); + Pair *pair = malloc(sizeof(Pair)); + pair->key = key; + pair->val = malloc(strlen(val) + 1); + strcpy(pair->val, val); int index = hashFunc(key); - hmap->buckets[index] = Pair; + hmap->buckets[index] = pair; } /* Операция удаления */ diff --git a/ru/codes/c/chapter_hashing/hash_map_open_addressing.c b/ru/codes/c/chapter_hashing/hash_map_open_addressing.c index 41eba6053..9e89a1b88 100644 --- a/ru/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/ru/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -114,7 +114,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // Если пара ключ-значение найдена, перезаписать val и вернуть if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) { free(hashMap->buckets[index]->val); - hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1)); + hashMap->buckets[index]->val = (char *)malloc(strlen(val) + 1); strcpy(hashMap->buckets[index]->val, val); hashMap->buckets[index]->val[strlen(val)] = '\0'; return; @@ -122,7 +122,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // Если пары ключ-значение нет, добавить ее Pair *pair = (Pair *)malloc(sizeof(Pair)); pair->key = key; - pair->val = (char *)malloc(sizeof(strlen(val) + 1)); + pair->val = (char *)malloc(strlen(val) + 1); strcpy(pair->val, val); pair->val[strlen(val)] = '\0'; diff --git a/ru/codes/c/chapter_sorting/counting_sort.c b/ru/codes/c/chapter_sorting/counting_sort.c index 5aba17388..d04d50332 100644 --- a/ru/codes/c/chapter_sorting/counting_sort.c +++ b/ru/codes/c/chapter_sorting/counting_sort.c @@ -45,7 +45,7 @@ void countingSort(int nums[], int size) { } // 2. Подсчитать число появлений каждой цифры // counter[num] обозначает число появлений num - int *counter = calloc(m, sizeof(int)); + int *counter = calloc(m + 1, sizeof(int)); for (int i = 0; i < size; i++) { counter[nums[i]]++; } diff --git a/zh-hant/codes/c/chapter_graph/graph_adjacency_list.c b/zh-hant/codes/c/chapter_graph/graph_adjacency_list.c index 35236eaa1..fba8e63b9 100644 --- a/zh-hant/codes/c/chapter_graph/graph_adjacency_list.c +++ b/zh-hant/codes/c/chapter_graph/graph_adjacency_list.c @@ -109,7 +109,10 @@ void removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) { /* 新增頂點 */ void addVertex(GraphAdjList *graph, Vertex *vet) { - assert(graph != NULL && graph->size < MAX_SIZE); + assert(graph != NULL); + if (findNode(graph, vet) != NULL) + return; + assert(graph->size < MAX_SIZE); AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode)); head->vertex = vet; head->next = NULL; @@ -121,27 +124,6 @@ void addVertex(GraphAdjList *graph, Vertex *vet) { void removeVertex(GraphAdjList *graph, Vertex *vet) { AdjListNode *node = findNode(graph, vet); assert(node != NULL); - // 在鄰接表中刪除頂點 vet 對應的鏈結串列 - AdjListNode *cur = node, *pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - free(pre); - } - // 走訪其他頂點的鏈結串列,刪除所有包含 vet 的邊 - for (int i = 0; i < graph->size; i++) { - cur = graph->heads[i]; - pre = NULL; - while (cur) { - pre = cur; - cur = cur->next; - if (cur && cur->vertex == vet) { - pre->next = cur->next; - free(cur); - break; - } - } - } // 將該頂點之後的頂點向前移動,以填補空缺 int i; for (i = 0; i < graph->size; i++) { @@ -152,6 +134,25 @@ void removeVertex(GraphAdjList *graph, Vertex *vet) { graph->heads[j] = graph->heads[j + 1]; } graph->size--; + // 走訪其他頂點的鏈結串列,刪除所有包含 vet 的邊 + for (int i = 0; i < graph->size; i++) { + AdjListNode *cur = graph->heads[i]; + while (cur->next != NULL) { + AdjListNode *next = cur->next; + if (next->vertex == vet) { + cur->next = next->next; + free(next); + } else { + cur = next; + } + } + } + // 在鄰接表中刪除頂點 vet 對應的鏈結串列 + while (node != NULL) { + AdjListNode *next = node->next; + free(node); + node = next; + } free(vet); } diff --git a/zh-hant/codes/c/chapter_hashing/array_hash_map.c b/zh-hant/codes/c/chapter_hashing/array_hash_map.c index 58fb8a200..abfb3a515 100644 --- a/zh-hant/codes/c/chapter_hashing/array_hash_map.c +++ b/zh-hant/codes/c/chapter_hashing/array_hash_map.c @@ -63,13 +63,13 @@ const char *get(const ArrayHashMap *hmap, const int key) { /* 新增操作 */ void put(ArrayHashMap *hmap, const int key, const char *val) { - Pair *Pair = malloc(sizeof(Pair)); - Pair->key = key; - Pair->val = malloc(strlen(val) + 1); - strcpy(Pair->val, val); + Pair *pair = malloc(sizeof(Pair)); + pair->key = key; + pair->val = malloc(strlen(val) + 1); + strcpy(pair->val, val); int index = hashFunc(key); - hmap->buckets[index] = Pair; + hmap->buckets[index] = pair; } /* 刪除操作 */ diff --git a/zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c b/zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c index 1df03417e..aaded8d3d 100644 --- a/zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -114,7 +114,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // 若找到鍵值對,則覆蓋 val 並返回 if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) { free(hashMap->buckets[index]->val); - hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1)); + hashMap->buckets[index]->val = (char *)malloc(strlen(val) + 1); strcpy(hashMap->buckets[index]->val, val); hashMap->buckets[index]->val[strlen(val)] = '\0'; return; @@ -122,7 +122,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) { // 若鍵值對不存在,則新增該鍵值對 Pair *pair = (Pair *)malloc(sizeof(Pair)); pair->key = key; - pair->val = (char *)malloc(sizeof(strlen(val) + 1)); + pair->val = (char *)malloc(strlen(val) + 1); strcpy(pair->val, val); pair->val[strlen(val)] = '\0'; diff --git a/zh-hant/codes/c/chapter_sorting/counting_sort.c b/zh-hant/codes/c/chapter_sorting/counting_sort.c index ba1759fab..ca6f2fc0d 100644 --- a/zh-hant/codes/c/chapter_sorting/counting_sort.c +++ b/zh-hant/codes/c/chapter_sorting/counting_sort.c @@ -45,7 +45,7 @@ void countingSort(int nums[], int size) { } // 2. 統計各數字的出現次數 // counter[num] 代表 num 的出現次數 - int *counter = calloc(m, sizeof(int)); + int *counter = calloc(m + 1, sizeof(int)); for (int i = 0; i < size; i++) { counter[nums[i]]++; }