|
|
|
@ -33,30 +33,30 @@ struct hashMapChaining {
|
|
|
|
|
typedef struct hashMapChaining hashMapChaining;
|
|
|
|
|
|
|
|
|
|
// 函数声明
|
|
|
|
|
void extend(hashMapChaining *hashmap);
|
|
|
|
|
void extend(hashMapChaining *hashMap);
|
|
|
|
|
|
|
|
|
|
/* 初始化桶数组 */
|
|
|
|
|
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->buckets = (Pair *)malloc(sizeof(Pair) * tableSize);
|
|
|
|
|
memset(hashMap->buckets, 0, sizeof(Pair) * tableSize);
|
|
|
|
|
|
|
|
|
|
hashmap->capacity = tableSize;
|
|
|
|
|
hashmap->size = 0;
|
|
|
|
|
hashmap->extendRatio = 2;
|
|
|
|
|
hashmap->loadThres = 2.0 / 3.0;
|
|
|
|
|
hashMap->capacity = tableSize;
|
|
|
|
|
hashMap->size = 0;
|
|
|
|
|
hashMap->extendRatio = 2;
|
|
|
|
|
hashMap->loadThres = 2.0 / 3.0;
|
|
|
|
|
|
|
|
|
|
return hashmap;
|
|
|
|
|
return hashMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 销毁哈希表 */
|
|
|
|
|
void delHashMapChaining(hashMapChaining *hashmap) {
|
|
|
|
|
for (int i = 0; i < hashmap->capacity; i++) {
|
|
|
|
|
Pair *pair = &hashmap->buckets[i];
|
|
|
|
|
void delHashMapChaining(hashMapChaining *hashMap) {
|
|
|
|
|
for (int i = 0; i < hashMap->capacity; i++) {
|
|
|
|
|
Pair *pair = &hashMap->buckets[i];
|
|
|
|
|
Node *node = pair->node;
|
|
|
|
|
while (node != NULL) {
|
|
|
|
|
Node *temp = node;
|
|
|
|
@ -65,24 +65,24 @@ void delHashMapChaining(hashMapChaining *hashmap) {
|
|
|
|
|
free(temp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(hashmap->buckets);
|
|
|
|
|
free(hashmap);
|
|
|
|
|
free(hashMap->buckets);
|
|
|
|
|
free(hashMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
|
int hashFunc(hashMapChaining *hashmap, const int key) {
|
|
|
|
|
return key % hashmap->capacity;
|
|
|
|
|
int hashFunc(hashMapChaining *hashMap, const int key) {
|
|
|
|
|
return key % hashMap->capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 负载因子 */
|
|
|
|
|
double loadFactor(hashMapChaining *hashmap) {
|
|
|
|
|
return (double)hashmap->size / (double)hashmap->capacity;
|
|
|
|
|
double loadFactor(hashMapChaining *hashMap) {
|
|
|
|
|
return (double)hashMap->size / (double)hashMap->capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
const char *get(hashMapChaining *hashmap, const int key) {
|
|
|
|
|
int index = hashFunc(hashmap, key);
|
|
|
|
|
Pair *pair = &hashmap->buckets[index];
|
|
|
|
|
char *get(hashMapChaining *hashMap, const int key) {
|
|
|
|
|
int index = hashFunc(hashMap, key);
|
|
|
|
|
Pair *pair = &hashMap->buckets[index];
|
|
|
|
|
Node *node = pair->node;
|
|
|
|
|
while (node != NULL) {
|
|
|
|
|
if (node->key == key) {
|
|
|
|
@ -94,11 +94,11 @@ const char *get(hashMapChaining *hashmap, const int key) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
void put(hashMapChaining *hashmap, const int key, char *val) {
|
|
|
|
|
if (loadFactor(hashmap) > hashmap->loadThres) {
|
|
|
|
|
extend(hashmap);
|
|
|
|
|
void put(hashMapChaining *hashMap, const int key, char *val) {
|
|
|
|
|
if (loadFactor(hashMap) > hashMap->loadThres) {
|
|
|
|
|
extend(hashMap);
|
|
|
|
|
}
|
|
|
|
|
int index = hashFunc(hashmap, key);
|
|
|
|
|
int index = hashFunc(hashMap, key);
|
|
|
|
|
|
|
|
|
|
// 先为新节点分配空间再赋值
|
|
|
|
|
Node *newNode = (Node *)malloc(sizeof(Node));
|
|
|
|
@ -108,11 +108,11 @@ void put(hashMapChaining *hashmap, const int key, char *val) {
|
|
|
|
|
strcpy(newNode->val, val);
|
|
|
|
|
newNode->val[strlen(val)] = '\0';
|
|
|
|
|
|
|
|
|
|
Pair *pair = &hashmap->buckets[index];
|
|
|
|
|
Pair *pair = &hashMap->buckets[index];
|
|
|
|
|
Node *node = pair->node;
|
|
|
|
|
if (node == NULL) {
|
|
|
|
|
hashmap->buckets[index].node = newNode;
|
|
|
|
|
hashmap->size++;
|
|
|
|
|
hashMap->buckets[index].node = newNode;
|
|
|
|
|
hashMap->size++;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while (node != NULL) {
|
|
|
|
@ -131,13 +131,13 @@ void put(hashMapChaining *hashmap, const int key, char *val) {
|
|
|
|
|
node = node->next;
|
|
|
|
|
}
|
|
|
|
|
node->next = newNode;
|
|
|
|
|
hashmap->size++;
|
|
|
|
|
hashMap->size++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
void removeItem(hashMapChaining *hashmap, int key) {
|
|
|
|
|
int index = hashFunc(hashmap, key);
|
|
|
|
|
Pair *pair = &hashmap->buckets[index];
|
|
|
|
|
void removeItem(hashMapChaining *hashMap, int key) {
|
|
|
|
|
int index = hashFunc(hashMap, key);
|
|
|
|
|
Pair *pair = &hashMap->buckets[index];
|
|
|
|
|
Node *node = pair->node;
|
|
|
|
|
// 保存后继的节点
|
|
|
|
|
Node *prev = NULL;
|
|
|
|
@ -152,7 +152,7 @@ void removeItem(hashMapChaining *hashmap, int key) {
|
|
|
|
|
// 释放内存
|
|
|
|
|
free(node->val);
|
|
|
|
|
free(node);
|
|
|
|
|
hashmap->size--;
|
|
|
|
|
hashMap->size--;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
prev = node;
|
|
|
|
@ -162,22 +162,22 @@ void removeItem(hashMapChaining *hashmap, int key) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 扩容哈希表 */
|
|
|
|
|
void extend(hashMapChaining *hashmap) {
|
|
|
|
|
void extend(hashMapChaining *hashMap) {
|
|
|
|
|
// 暂存原哈希表
|
|
|
|
|
Pair *oldBuckets = hashmap->buckets;
|
|
|
|
|
int oldCapacity = hashmap->capacity;
|
|
|
|
|
Pair *oldBuckets = hashMap->buckets;
|
|
|
|
|
int oldCapacity = hashMap->capacity;
|
|
|
|
|
|
|
|
|
|
// 创建新的哈希表,重新分配一段空间
|
|
|
|
|
hashmap->capacity *= hashmap->extendRatio;
|
|
|
|
|
hashmap->buckets = (Pair *)malloc(sizeof(Pair) * hashmap->capacity);
|
|
|
|
|
memset(hashmap->buckets, 0, sizeof(Pair) * hashmap->capacity);
|
|
|
|
|
hashmap->size = 0;
|
|
|
|
|
hashMap->capacity *= hashMap->extendRatio;
|
|
|
|
|
hashMap->buckets = (Pair *)malloc(sizeof(Pair) * hashMap->capacity);
|
|
|
|
|
memset(hashMap->buckets, 0, sizeof(Pair) * hashMap->capacity);
|
|
|
|
|
hashMap->size = 0;
|
|
|
|
|
|
|
|
|
|
// 将原哈希表中的键值对重新哈希到新的哈希表中
|
|
|
|
|
for (int i = 0; i < oldCapacity; i++) {
|
|
|
|
|
Node *node = oldBuckets[i].node;
|
|
|
|
|
while (node != NULL) {
|
|
|
|
|
put(hashmap, node->key, node->val);
|
|
|
|
|
put(hashMap, node->key, node->val);
|
|
|
|
|
node = node->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -196,10 +196,10 @@ void extend(hashMapChaining *hashmap) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
|
void print(hashMapChaining *hashmap) {
|
|
|
|
|
for (int i = 0; i < hashmap->capacity; i++) {
|
|
|
|
|
void print(hashMapChaining *hashMap) {
|
|
|
|
|
for (int i = 0; i < hashMap->capacity; i++) {
|
|
|
|
|
printf("[");
|
|
|
|
|
Pair *pair = &hashmap->buckets[i];
|
|
|
|
|
Pair *pair = &hashMap->buckets[i];
|
|
|
|
|
Node *node = pair->node;
|
|
|
|
|
while (node != NULL) {
|
|
|
|
|
if (node->val != NULL) {
|
|
|
|
|