转自:网络hashmap 之链地址法1、定义哈希表 及 哈希桶 结构体#include #include #include // 定义哈希桶的节点结构体typedef struct Node { char* key; int value; struct Node* next;} Node;// 定义哈希表结构体typedef struct HashMap { int size; Node** buckets;} HashMap;2、创建指定大小的哈希表// 创建指定大小的哈希表HashMap* createHashMap(int size) { HashMap* map = (HashMap*)malloc(sizeof(HashMap)); map->size = size; map->buckets = (Node**)calloc(size, sizeof(Node*)); return map;}3、哈希函数// 哈希函数int hash(HashMap* map, char* key) { int sum = 0; for (int i = 0; i strlen(key); i++) { sum += key[i]; } return sum % map->size;}4、HashMap put操作void put(HashMap* map, char* key, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->key = strdup(key); newNode->value = value; newNode->next = NULL; int index = hash(map, key); Node* curr = map->buckets[in
………………………………