建立简单的Hash table(哈希表)by C language

 

 1 #define SIZE 1000    //定义Hash table的初始大小
 2 struct HashArray
 3 {
 4     int key;
 5     int count;
 6     struct HashArray* next;
 7 }Hash[SIZE];       //主函数中需要初始化
 8 void addHash(int num)     //在Hash table中添加数据
 9 {
10     int temp=abs(num%SIZE);     //添加的数据可包括负数
11     if(Hash[temp].key==0)
12     {
13         Hash[temp].key=num;
14         Hash[temp].count++;
15     }else if(Hash[temp].key==num)
16     {
17         Hash[temp].count++;     
18     }else
19     {
20         struct HashArray *p=&Hash[temp]; 
21         while(p->key!=num&&p->next!=NULL)    
22         {p=p->next;}
23         if(p->key==num)
24         {p->count++;}
25         else
26         {
27             p->next=(struct HashArray*)malloc(sizeof(struct HashArray));
28             p=p->next;
29             p->key=num;
30             p->count=1;
31             p->next=NULL;
32         }
33     }   
34 } 

 

转载于:https://www.cnblogs.com/Blue-Keroro/p/7819472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单哈希表实现,包括建立和查找操作。以下是示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 10 typedef struct _hash_node { char *key; int value; struct _hash_node *next; } HashNode; typedef struct _hash_table { HashNode *table[MAX_SIZE]; } HashTable; unsigned int hash_function(const char *key) { unsigned int hash = 0; for (int i = 0; i < strlen(key); i++) { hash = hash * 31 + key[i]; } return hash % MAX_SIZE; } HashNode *create_node(const char *key, int value) { HashNode *new_node = (HashNode *) malloc(sizeof(HashNode)); new_node->key = strdup(key); new_node->value = value; new_node->next = NULL; return new_node; } void insert(HashTable *hash_table, const char *key, int value) { unsigned int hash = hash_function(key); HashNode *node = hash_table->table[hash]; while (node != NULL) { if (strcmp(node->key, key) == 0) { node->value = value; // replace value return; } node = node->next; } HashNode *new_node = create_node(key, value); new_node->next = hash_table->table[hash]; hash_table->table[hash] = new_node; } int get(HashTable *hash_table, const char *key) { unsigned int hash = hash_function(key); HashNode *node = hash_table->table[hash]; while (node != NULL) { if (strcmp(node->key, key) == 0) { return node->value; } node = node->next; } return -1; // key not found } int main() { HashTable hash_table; memset(&hash_table, 0, sizeof(HashTable)); insert(&hash_table, "apple", 1); insert(&hash_table, "banana", 2); insert(&hash_table, "orange", 3); printf("%d\n", get(&hash_table, "apple")); // 输出 1 printf("%d\n", get(&hash_table, "banana")); // 输出 2 printf("%d\n", get(&hash_table, "orange")); // 输出 3 printf("%d\n", get(&hash_table, "watermelon")); // 输出 -1 return 0; } ``` 在上面的代码中,我们使用哈希函数将键映射到哈希表中的索引位置,然后使用链表方法解决哈希冲突。`create_node`函数用于创建哈希表中的节点,`insert`函数用于将键和值插入哈希表中,如果哈希表中已经存在该键,则更新其对应的值;`get`函数用于根据键查找哈希表中的值。 注意:在实际使用中,我们需要处理哈希冲突,这里使用了链表法,可以有效地解决冲突问题。 下面是一个使用示例: ``` 1 2 3 -1 ``` 在上面的示例中,我们创建了一个哈希表,并向其中插入了三个键值对。然后我们使用`get`函数来查找键对应的值。如果键不存在于哈希表中,则返回-1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值