refactor: Improve the hash_map_chaining.c implementation. (#858)
* Improve the hash_map_chaining.c implementation. * Update hash_map_chaining.c
This commit is contained in:
parent
84e2799f1b
commit
3df5c36370
@ -1,67 +1,62 @@
|
|||||||
/**
|
/**
|
||||||
* File: hash_map_chaining.c
|
* File: hash_map_chaining.c
|
||||||
* Created Time: 2023-08-29
|
* Created Time: 2023-10-13
|
||||||
* Author: SenMing (1206575349@qq.com)
|
* Author: SenMing (1206575349@qq.com), Krahets (krahets@163.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../utils/common.h"
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
/* 键值对 int->string */
|
#include <string.h>
|
||||||
struct node {
|
|
||||||
int key;
|
|
||||||
char *val;
|
|
||||||
struct node *next; // 下一个元素
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct node Node;
|
|
||||||
|
|
||||||
|
/* 键值对 */
|
||||||
struct pair {
|
struct pair {
|
||||||
Node *node; // 键值对节点
|
int key;
|
||||||
|
char val[100]; // 假设 val 最大长度为 100
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct pair Pair;
|
typedef struct pair Pair;
|
||||||
|
|
||||||
/* 基于数组简易实现的链式地址哈希表 */
|
/* 链表节点 */
|
||||||
|
struct node {
|
||||||
|
Pair *pair;
|
||||||
|
struct Node *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct node Node;
|
||||||
|
|
||||||
|
/* 链式地址哈希表 */
|
||||||
struct hashMapChaining {
|
struct hashMapChaining {
|
||||||
int size; // 键值对数量
|
int size; // 键值对数量
|
||||||
int capacity; // 哈希表容量
|
int capacity; // 哈希表容量
|
||||||
double loadThres; // 触发扩容的负载因子阈值
|
double loadThres; // 触发扩容的负载因子阈值
|
||||||
int extendRatio; // 扩容倍数
|
int extendRatio; // 扩容倍数
|
||||||
Pair *buckets; // 桶数组
|
Node **buckets; // 桶数组
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct hashMapChaining hashMapChaining;
|
typedef struct hashMapChaining hashMapChaining;
|
||||||
|
|
||||||
// 函数声明
|
/* 构造方法 */
|
||||||
void extend(hashMapChaining *hashMap);
|
hashMapChaining *initHashMapChaining() {
|
||||||
|
|
||||||
/* 初始化桶数组 */
|
|
||||||
hashMapChaining *newHashMapChaining() {
|
|
||||||
// 为哈希表分配空间
|
|
||||||
int tableSize = 4;
|
|
||||||
hashMapChaining *hashMap = (hashMapChaining *)malloc(sizeof(hashMapChaining));
|
hashMapChaining *hashMap = (hashMapChaining *)malloc(sizeof(hashMapChaining));
|
||||||
|
|
||||||
// 初始化数组
|
|
||||||
hashMap->buckets = (Pair *)malloc(sizeof(Pair) * tableSize);
|
|
||||||
memset(hashMap->buckets, 0, sizeof(Pair) * tableSize);
|
|
||||||
|
|
||||||
hashMap->capacity = tableSize;
|
|
||||||
hashMap->size = 0;
|
hashMap->size = 0;
|
||||||
hashMap->extendRatio = 2;
|
hashMap->capacity = 4;
|
||||||
hashMap->loadThres = 2.0 / 3.0;
|
hashMap->loadThres = 2.0 / 3.0;
|
||||||
|
hashMap->extendRatio = 2;
|
||||||
|
hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));
|
||||||
|
for (int i = 0; i < hashMap->capacity; i++) {
|
||||||
|
hashMap->buckets[i] = NULL;
|
||||||
|
}
|
||||||
return hashMap;
|
return hashMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 销毁哈希表 */
|
/* 析构方法 */
|
||||||
void delHashMapChaining(hashMapChaining *hashMap) {
|
void freeHashMapChaining(hashMapChaining *hashMap) {
|
||||||
for (int i = 0; i < hashMap->capacity; i++) {
|
for (int i = 0; i < hashMap->capacity; i++) {
|
||||||
Pair *pair = &hashMap->buckets[i];
|
Node *cur = hashMap->buckets[i];
|
||||||
Node *node = pair->node;
|
while (cur) {
|
||||||
while (node != NULL) {
|
Node *temp = cur;
|
||||||
Node *temp = node;
|
cur = cur->next;
|
||||||
node = node->next;
|
free(temp->pair);
|
||||||
free(temp->val);
|
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,7 +65,7 @@ void delHashMapChaining(hashMapChaining *hashMap) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 哈希函数 */
|
/* 哈希函数 */
|
||||||
int hashFunc(hashMapChaining *hashMap, const int key) {
|
int hashFunc(hashMapChaining *hashMap, int key) {
|
||||||
return key % hashMap->capacity;
|
return key % hashMap->capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,164 +75,142 @@ double loadFactor(hashMapChaining *hashMap) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
char *get(hashMapChaining *hashMap, const int key) {
|
char *get(hashMapChaining *hashMap, int key) {
|
||||||
int index = hashFunc(hashMap, key);
|
int index = hashFunc(hashMap, key);
|
||||||
Pair *pair = &hashMap->buckets[index];
|
// 遍历桶,若找到 key 则返回对应 val
|
||||||
Node *node = pair->node;
|
Node *cur = hashMap->buckets[index];
|
||||||
while (node != NULL) {
|
while (cur) {
|
||||||
if (node->key == key) {
|
if (cur->pair->key == key) {
|
||||||
return node->val;
|
return cur->pair->val;
|
||||||
}
|
}
|
||||||
node = node->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
return NULL;
|
return ""; // 若未找到 key 则返回空字符串
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
void put(hashMapChaining *hashMap, const int key, char *val) {
|
void put(hashMapChaining *hashMap, int key, const char *val);
|
||||||
if (loadFactor(hashMap) > hashMap->loadThres) {
|
|
||||||
extend(hashMap);
|
|
||||||
}
|
|
||||||
int index = hashFunc(hashMap, key);
|
|
||||||
|
|
||||||
// 先为新节点分配空间再赋值
|
|
||||||
Node *newNode = (Node *)malloc(sizeof(Node));
|
|
||||||
memset(newNode, 0, sizeof(Node));
|
|
||||||
newNode->key = key;
|
|
||||||
newNode->val = (char *)malloc(strlen(val) + 1);
|
|
||||||
strcpy(newNode->val, val);
|
|
||||||
newNode->val[strlen(val)] = '\0';
|
|
||||||
|
|
||||||
Pair *pair = &hashMap->buckets[index];
|
|
||||||
Node *node = pair->node;
|
|
||||||
if (node == NULL) {
|
|
||||||
hashMap->buckets[index].node = newNode;
|
|
||||||
hashMap->size++;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
while (node != NULL) {
|
|
||||||
if (node->key == key) {
|
|
||||||
// 释放先前分配的内存
|
|
||||||
free(node->val);
|
|
||||||
// 更新节点的值
|
|
||||||
node->val = (char *)malloc(strlen(val) + 1);
|
|
||||||
strcpy(node->val, val);
|
|
||||||
node->val[strlen(val)] = '\0';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (node->next == NULL) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
node->next = newNode;
|
|
||||||
hashMap->size++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 删除操作 */
|
|
||||||
void removeItem(hashMapChaining *hashMap, int key) {
|
|
||||||
int index = hashFunc(hashMap, key);
|
|
||||||
Pair *pair = &hashMap->buckets[index];
|
|
||||||
Node *node = pair->node;
|
|
||||||
// 保存后继的节点
|
|
||||||
Node *prev = NULL;
|
|
||||||
while (node != NULL) {
|
|
||||||
if (node->key == key) {
|
|
||||||
// 如果要删除的节点是桶的第一个节点
|
|
||||||
if (prev == NULL) {
|
|
||||||
pair->node = node->next;
|
|
||||||
} else {
|
|
||||||
prev->next = node->next;
|
|
||||||
}
|
|
||||||
// 释放内存
|
|
||||||
free(node->val);
|
|
||||||
free(node);
|
|
||||||
hashMap->size--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
prev = node;
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 扩容哈希表 */
|
/* 扩容哈希表 */
|
||||||
void extend(hashMapChaining *hashMap) {
|
void extend(hashMapChaining *hashMap) {
|
||||||
// 暂存原哈希表
|
// 暂存原哈希表
|
||||||
Pair *oldBuckets = hashMap->buckets;
|
|
||||||
int oldCapacity = hashMap->capacity;
|
int oldCapacity = hashMap->capacity;
|
||||||
|
Node **oldBuckets = hashMap->buckets;
|
||||||
// 创建新的哈希表,重新分配一段空间
|
// 初始化扩容后的新哈希表
|
||||||
hashMap->capacity *= hashMap->extendRatio;
|
hashMap->capacity *= hashMap->extendRatio;
|
||||||
hashMap->buckets = (Pair *)malloc(sizeof(Pair) * hashMap->capacity);
|
hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));
|
||||||
memset(hashMap->buckets, 0, sizeof(Pair) * hashMap->capacity);
|
for (int i = 0; i < hashMap->capacity; i++) {
|
||||||
hashMap->size = 0;
|
hashMap->buckets[i] = NULL;
|
||||||
|
|
||||||
// 将原哈希表中的键值对重新哈希到新的哈希表中
|
|
||||||
for (int i = 0; i < oldCapacity; i++) {
|
|
||||||
Node *node = oldBuckets[i].node;
|
|
||||||
while (node != NULL) {
|
|
||||||
put(hashMap, node->key, node->val);
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
hashMap->size = 0;
|
||||||
// 释放原哈希表的内存
|
// 将键值对从原哈希表搬运至新哈希表
|
||||||
for (int i = 0; i < oldCapacity; i++) {
|
for (int i = 0; i < oldCapacity; i++) {
|
||||||
Node *node = oldBuckets[i].node;
|
Node *cur = oldBuckets[i];
|
||||||
while (node != NULL) {
|
while (cur) {
|
||||||
Node *temp = node;
|
put(hashMap, cur->pair->key, cur->pair->val);
|
||||||
node = node->next;
|
Node *temp = cur;
|
||||||
free(temp->val);
|
cur = cur->next;
|
||||||
|
// 释放内存
|
||||||
|
free(temp->pair);
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(oldBuckets);
|
free(oldBuckets);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
void put(hashMapChaining *hashMap, int key, const char *val) {
|
||||||
|
// 当负载因子超过阈值时,执行扩容
|
||||||
|
if (loadFactor(hashMap) > hashMap->loadThres) {
|
||||||
|
extend(hashMap);
|
||||||
|
}
|
||||||
|
int index = hashFunc(hashMap, key);
|
||||||
|
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
||||||
|
Node *cur = hashMap->buckets[index];
|
||||||
|
while (cur) {
|
||||||
|
if (cur->pair->key == key) {
|
||||||
|
strcpy(cur->pair->val, val); // 若遇到指定 key ,则更新对应 val 并返回
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
// 若无该 key ,则将键值对添加至尾部
|
||||||
|
Pair *newPair = (Pair *)malloc(sizeof(Pair));
|
||||||
|
newPair->key = key;
|
||||||
|
strcpy(newPair->val, val);
|
||||||
|
Node *newNode = (Node *)malloc(sizeof(Node));
|
||||||
|
newNode->pair = newPair;
|
||||||
|
newNode->next = hashMap->buckets[index];
|
||||||
|
hashMap->buckets[index] = newNode;
|
||||||
|
hashMap->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
void removeKey(hashMapChaining *hashMap, int key) {
|
||||||
|
int index = hashFunc(hashMap, key);
|
||||||
|
Node *cur = hashMap->buckets[index];
|
||||||
|
Node *pre = NULL;
|
||||||
|
while (cur) {
|
||||||
|
if (cur->pair->key == key) {
|
||||||
|
// 从中删除键值对
|
||||||
|
if (pre) {
|
||||||
|
pre->next = cur->next;
|
||||||
|
} else {
|
||||||
|
hashMap->buckets[index] = cur->next;
|
||||||
|
}
|
||||||
|
// 释放内存
|
||||||
|
free(cur->pair);
|
||||||
|
free(cur);
|
||||||
|
hashMap->size--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pre = cur;
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
void print(hashMapChaining *hashMap) {
|
void print(hashMapChaining *hashMap) {
|
||||||
for (int i = 0; i < hashMap->capacity; i++) {
|
for (int i = 0; i < hashMap->capacity; i++) {
|
||||||
|
Node *cur = hashMap->buckets[i];
|
||||||
printf("[");
|
printf("[");
|
||||||
Pair *pair = &hashMap->buckets[i];
|
while (cur) {
|
||||||
Node *node = pair->node;
|
printf("%d -> %s, ", cur->pair->key, cur->pair->val);
|
||||||
while (node != NULL) {
|
cur = cur->next;
|
||||||
if (node->val != NULL) {
|
|
||||||
printf("%d->%s, ", node->key, node->val);
|
|
||||||
}
|
|
||||||
node = node->next;
|
|
||||||
}
|
}
|
||||||
printf("]\n");
|
printf("]\n");
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化哈希表 */
|
/* 初始化哈希表 */
|
||||||
hashMapChaining *map = newHashMapChaining();
|
hashMapChaining *hashMap = initHashMapChaining();
|
||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
// 在哈希表中添加键值对 (key, value)
|
// 在哈希表中添加键值对 (key, value)
|
||||||
put(map, 12836, "小哈");
|
put(hashMap, 12836, "小哈");
|
||||||
put(map, 15937, "小啰");
|
put(hashMap, 15937, "小啰");
|
||||||
put(map, 16750, "小算");
|
put(hashMap, 16750, "小算");
|
||||||
put(map, 13276, "小法");
|
put(hashMap, 13276, "小法");
|
||||||
put(map, 10583, "小鸭");
|
put(hashMap, 10583, "小鸭");
|
||||||
printf("\n添加完成后,哈希表为\nKey -> Value\n");
|
printf("\n添加完成后,哈希表为\nKey -> Value\n");
|
||||||
print(map);
|
print(hashMap);
|
||||||
|
|
||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
// 向哈希表输入键 key ,得到值 value
|
// 向哈希表输入键 key ,得到值 value
|
||||||
const char *name = get(map, 13276);
|
char *name = get(hashMap, 13276);
|
||||||
printf("\n输入学号 13276 ,查询到姓名 %s\n", name);
|
printf("\n输入学号 13276 ,查询到姓名 %s\n", name);
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
// 在哈希表中删除键值对 (key, value)
|
// 在哈希表中删除键值对 (key, value)
|
||||||
removeItem(map, 12836);
|
removeKey(hashMap, 12836);
|
||||||
printf("\n删除 12836 后,哈希表为\nKey -> Value\n");
|
printf("\n删除学号 12836 后,哈希表为\nKey -> Value\n");
|
||||||
print(map);
|
print(hashMap);
|
||||||
|
|
||||||
|
/* 释放哈希表空间 */
|
||||||
|
freeHashMapChaining(hashMap);
|
||||||
|
|
||||||
delHashMapChaining(map);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -94,6 +94,8 @@
|
|||||||
=== "C"
|
=== "C"
|
||||||
|
|
||||||
```c title="hash_map_chaining.c"
|
```c title="hash_map_chaining.c"
|
||||||
|
[class]{node}-[func]{}
|
||||||
|
|
||||||
[class]{hashMapChaining}-[func]{}
|
[class]{hashMapChaining}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user