C语言中的hashtable哈希表的定义和使用场景
时间: 2024-04-10 09:28:24 浏览: 27
在C语言中,哈希表(Hashtable)是一种常见的数据结构,用于快速存储和检索数据。它基于哈希函数将键(Key)映射到存储桶(Bucket)中,每个桶中存储一个键值对。下面是一个简单的哈希表的定义和使用场景:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 100
typedef struct {
char* key;
int value;
} KeyValue;
typedef struct {
KeyValue* data[TABLE_SIZE];
} Hashtable;
Hashtable* createHashtable() {
Hashtable* hashtable = (Hashtable*)malloc(sizeof(Hashtable));
memset(hashtable->data, 0, sizeof(hashtable->data));
return hashtable;
}
unsigned int hash(const char* key) {
unsigned int hashValue = 0;
while (*key) {
hashValue = (hashValue << 5) + *key++;
}
return hashValue % TABLE_SIZE;
}
void insert(Hashtable* hashtable, const char* key, int value) {
unsigned int index = hash(key);
KeyValue* keyValue = (KeyValue*)malloc(sizeof(KeyValue));
keyValue->key = strdup(key);
keyValue->value = value;
hashtable->data[index] = keyValue;
}
int find(Hashtable* hashtable, const char* key) {
unsigned int index = hash(key);
KeyValue* keyValue = hashtable->data[index];
if (keyValue && strcmp(keyValue->key, key) == 0) {
return keyValue->value;
}
return -1;
}
void destroyHashtable(Hashtable* hashtable) {
for (int i = 0; i < TABLE_SIZE; i++) {
KeyValue* keyValue = hashtable->data[i];
if (keyValue) {
free(keyValue->key);
free(keyValue);
}
}
free(hashtable);
}
int main() {
Hashtable* hashtable = createHashtable();
// 插入键值对
insert(hashtable, "apple", 5);
insert(hashtable, "banana", 3);
insert(hashtable, "orange", 7);
// 查找值
printf("The value of apple is: %d\n", find(hashtable, "apple"));
printf("The value of banana is: %d\n", find(hashtable, "banana"));
printf("The value of orange is: %d\n", find(hashtable, "orange"));
destroyHashtable(hashtable);
return 0;
}
```
这个示例代码实现了一个简单的哈希表,可以插入键值对和查找对应的值。通过`createHashtable`函数可以创建一个空的哈希表,通过`insert`函数可以插入一个键值对,通过`find`函数可以查找对应键的值,通过`destroyHashtable`函数可以销毁哈希表。
哈希表的使用场景包括:
1. 缓存:哈希表可以用于快速存储和检索缓存数据,提高访问速度。
2. 字典:哈希表可以用于实现字典,将键映射到对应的值。
3. 数据索引:哈希表可以用于构建数据索引,根据键快速查找对应的数据。
4. 频率统计:哈希表可以用于统计数据的频率,记录某个元素出现的次数。
希望对你有帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)