Fix C allocations and adjacency-list vertex removal (#1997)

Co-authored-by: Lukas <103962359+L4XB@users.noreply.github.com>
This commit is contained in:
Lukas
2026-11-23 18:24:12 +01:00
committed by GitHub
parent 0d4b779b87
commit d5ac60d5d9
24 changed files with 186 additions and 180 deletions

View File

@@ -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);
}

View File

@@ -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;
}
/* 删除操作 */

View File

@@ -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';

View File

@@ -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]]++;
}

View File

@@ -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);
}

View File

@@ -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 */

View File

@@ -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';

View File

@@ -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]]++;
}

View File

@@ -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);
}

View File

@@ -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;
}
/* 削除操作 */

View File

@@ -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';

View File

@@ -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]]++;
}

View File

@@ -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);
}

View File

@@ -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;
}
/* 삭제 연산 */

View File

@@ -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';

View File

@@ -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]]++;
}

View File

@@ -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);
}

View File

@@ -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;
}
/* Операция удаления */

View File

@@ -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';

View File

@@ -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]]++;
}

View File

@@ -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);
}

View File

@@ -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;
}
/* 刪除操作 */

View File

@@ -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';

View File

@@ -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]]++;
}