hello-algo/codes/c/chapter_hashing/array_hash_map.c

213 lines
5 KiB
C
Raw Normal View History

/**
* File: array_hash_map.c
* Created Time: 2023-03-18
* Author: Guanngxu (446678850@qq.com)
*/
#include "../utils/common.h"
/* 哈希表默认大小 */
2023-11-01 05:14:22 +08:00
#define HASHTABLE_CAPACITY 100
/* 键值对 int->string */
typedef struct {
int key;
char *val;
} Pair;
/* 键值对的集合 */
typedef struct {
void *set;
int len;
} MapSet;
/* 基于数组实现的哈希表 */
typedef struct {
2023-11-01 05:14:22 +08:00
Pair *buckets[HASHTABLE_CAPACITY];
} ArrayHashMap;
2023-11-01 05:14:22 +08:00
/* 构造函数 */
ArrayHashMap *newArrayHashMap() {
2023-11-01 05:14:22 +08:00
ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
return hmap;
}
/* 析构函数 */
void delArrayHashMap(ArrayHashMap *hmap) {
for (int i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
free(hmap->buckets[i]->val);
free(hmap->buckets[i]);
}
}
free(hmap);
}
/* 哈希函数 */
int hashFunc(int key) {
2023-11-01 05:14:22 +08:00
int index = key % HASHTABLE_CAPACITY;
return index;
}
/* 查询操作 */
2023-11-01 05:14:22 +08:00
const char *get(const ArrayHashMap *hmap, const int key) {
int index = hashFunc(key);
2023-11-01 05:14:22 +08:00
const Pair *Pair = hmap->buckets[index];
if (Pair == NULL)
return NULL;
return Pair->val;
}
/* 添加操作 */
2023-11-01 05:14:22 +08:00
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);
int index = hashFunc(key);
2023-11-01 05:14:22 +08:00
hmap->buckets[index] = Pair;
}
/* 删除操作 */
2023-11-01 05:14:22 +08:00
void removeItem(ArrayHashMap *hmap, const int key) {
int index = hashFunc(key);
2023-11-01 05:14:22 +08:00
free(hmap->buckets[index]->val);
free(hmap->buckets[index]);
hmap->buckets[index] = NULL;
}
/* 获取所有键值对 */
2023-11-01 05:14:22 +08:00
void pairSet(ArrayHashMap *hmap, MapSet *set) {
Pair *entries;
int i = 0, index = 0;
int total = 0;
/* 统计有效键值对数量 */
2023-11-01 05:14:22 +08:00
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
total++;
}
}
entries = malloc(sizeof(Pair) * total);
2023-11-01 05:14:22 +08:00
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
entries[index].key = hmap->buckets[i]->key;
fix: bug fixes for array_hash_map.c and counting_sort.c (#968) * fix(codes/cpp): Memory leak fix: the space was not freed when pop removed the element. * fix(codes/cpp): Fix access error when printArray(arr, 0) * Update PrintUtil.hpp * fix(codes/c): Fix some errors of cmake build * feat(codes/c): Add hashing_search.c * styles(codes/c): Modify function description * styles(codes/c): Modify binary_search.c code style * fix(codes/c): Fix the problem in binary_tree_bfs.c and the problem that the memory is not released. * feat: Add preorder_traversal_i_compact.c * feat(codes/c): Add head_sort.c * feat(codes/c): Add bucket_sort.c * feat(codes/c): Add binary_search_edge.c * fix(codes/c): Add programs that are not managed by cmake (c code) * feat(codes/c): Add selection_sort.c * style(codes/c): Change swap in selection_sort.c to `selectionSort` * styles(codes/c): Change style. * fix(codes/c): Fix some formatting errors and temporarily remove backtracking chapters * fix(codes/c): Fix space_complexity.c build error. * feat(codes/c): Add array_binary_tree.c * feat(code/c): Update push_back and pop_back in vector.h * styles(codes/c): Adjust format. * feat(codes/c): Add `interation.c ` `recursion.c` `simple_hash.c` `binary_search_edge.c` `binary_search_insertion.c` in C codes. * fix(mylist.c): Fix `insert` function in `mylist.c` https://github.com/krahets/hello-algo/discussions/32#discussioncomment-6974163 * feat(codes/c): Add binary_search_recur.c * fix(codes/c): Bug fixes in discussion https://github.com/krahets/hello-algo/discussions/78\#discussioncomment-7611511 https://github.com/krahets/hello-algo/discussions/428\#discussioncomment-7637613 --------- Co-authored-by: Yudong Jin <krahets@163.com>
2023-11-26 22:56:30 +08:00
entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);
2023-11-01 05:14:22 +08:00
strcpy(entries[index].val, hmap->buckets[i]->val);
index++;
}
}
set->set = entries;
set->len = total;
}
/* 获取所有键 */
2023-11-01 05:14:22 +08:00
void keySet(ArrayHashMap *hmap, MapSet *set) {
int *keys;
int i = 0, index = 0;
int total = 0;
/* 统计有效键值对数量 */
2023-11-01 05:14:22 +08:00
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
total++;
}
}
keys = malloc(total * sizeof(int));
2023-11-01 05:14:22 +08:00
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
keys[index] = hmap->buckets[i]->key;
index++;
}
}
set->set = keys;
set->len = total;
}
/* 获取所有值 */
2023-11-01 05:14:22 +08:00
void valueSet(ArrayHashMap *hmap, MapSet *set) {
char **vals;
int i = 0, index = 0;
int total = 0;
/* 统计有效键值对数量 */
2023-11-01 05:14:22 +08:00
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
total++;
}
}
vals = malloc(total * sizeof(char *));
2023-11-01 05:14:22 +08:00
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
vals[index] = hmap->buckets[i]->val;
index++;
}
}
set->set = vals;
set->len = total;
}
/* 打印哈希表 */
2023-11-01 05:14:22 +08:00
void print(ArrayHashMap *hmap) {
int i;
MapSet set;
2023-11-01 05:14:22 +08:00
pairSet(hmap, &set);
Pair *entries = (Pair *)set.set;
for (i = 0; i < set.len; i++) {
printf("%d -> %s\n", entries[i].key, entries[i].val);
}
free(set.set);
}
/* Driver Code */
int main() {
/* 初始化哈希表 */
2023-11-01 05:14:22 +08:00
ArrayHashMap *hmap = newArrayHashMap();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
2023-11-01 05:14:22 +08:00
put(hmap, 12836, "小哈");
put(hmap, 15937, "小啰");
put(hmap, 16750, "小算");
put(hmap, 13276, "小法");
put(hmap, 10583, "小鸭");
printf("\n添加完成后,哈希表为\nKey -> Value\n");
2023-11-01 05:14:22 +08:00
print(hmap);
/* 查询操作 */
// 向哈希表中输入键 key ,得到值 value
2023-11-01 05:14:22 +08:00
const char *name = get(hmap, 15937);
printf("\n输入学号 15937 ,查询到姓名 %s\n", name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
2023-11-01 05:14:22 +08:00
removeItem(hmap, 10583);
printf("\n删除 10583 后,哈希表为\nKey -> Value\n");
2023-11-01 05:14:22 +08:00
print(hmap);
/* 遍历哈希表 */
int i;
printf("\n遍历键值对 Key->Value\n");
2023-11-01 05:14:22 +08:00
print(hmap);
MapSet set;
2023-11-01 05:14:22 +08:00
keySet(hmap, &set);
int *keys = (int *)set.set;
printf("\n单独遍历键 Key\n");
for (i = 0; i < set.len; i++) {
printf("%d\n", keys[i]);
}
free(set.set);
2023-11-01 05:14:22 +08:00
valueSet(hmap, &set);
char **vals = (char **)set.set;
printf("\n单独遍历键 Value\n");
for (i = 0; i < set.len; i++) {
printf("%s\n", vals[i]);
}
free(set.set);
2023-11-01 05:14:22 +08:00
delArrayHashMap(hmap);
return 0;
}