C语言中的两种hash表的实现

1、用数组

 

 

 

2、用链表

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
遍历链的基本方法是使用指针,从链的头节点开始依次遍历每个节点,直到遍历到链的尾节点为止。使用BKDRHash的方法可以将链的每个节点以哈希表的形式组织起来,这样可以更加高效地查找和访问链的节点。 遍历使用BKDRHash组织的链时,需要先定位到哈希表相应的桶,然后依次访问桶的每个节点即可。具体的实现方法可以参考下面的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define HASH_SIZE 10000 // 定义链节点的结构体 typedef struct ListNode { char* key; char* value; struct ListNode* next; } ListNode; // 定义哈希表的结构体 typedef struct HashTable { ListNode* buckets[HASH_SIZE]; } HashTable; // 计算字符串的哈希值 unsigned int BKDRHash(char* str) { unsigned int seed = 131; // 31、33、131、137、65537等常用的哈希种子 unsigned int hash = 0; while (*str) { hash = hash * seed + (*str++); } return hash % HASH_SIZE; } // 添加节点到哈希表 void HashTablePut(HashTable* table, char* key, char* value) { unsigned int hash = BKDRHash(key); ListNode* node = table->buckets[hash]; while (node) { if (strcmp(node->key, key) == 0) { // 如果key已经存在,则更新value free(node->value); node->value = strdup(value); return; } node = node->next; } // 如果key不存在,则创建新节点并插入到桶 node = (ListNode*)malloc(sizeof(ListNode)); node->key = strdup(key); node->value = strdup(value); node->next = table->buckets[hash]; table->buckets[hash] = node; } // 根据key查找节点的value char* HashTableGet(HashTable* table, char* key) { unsigned int hash = BKDRHash(key); ListNode* node = table->buckets[hash]; while (node) { if (strcmp(node->key, key) == 0) { return node->value; } node = node->next; } return NULL; } // 遍历哈希表的节点 void HashTableTraverse(HashTable* table) { for (int i = 0; i < HASH_SIZE; i++) { ListNode* node = table->buckets[i]; while (node) { printf("%s:%s\n", node->key, node->value); node = node->next; } } } // 释放哈希表的所有节点 void HashTableFree(HashTable* table) { for (int i = 0; i < HASH_SIZE; i++) { ListNode* node = table->buckets[i]; while (node) { ListNode* next = node->next; free(node->key); free(node->value); free(node); node = next; } table->buckets[i] = NULL; } } int main() { HashTable table = {0}; // 添加节点 HashTablePut(&table, "apple", "red"); HashTablePut(&table, "banana", "yellow"); HashTablePut(&table, "orange", "orange"); HashTablePut(&table, "grape", "purple"); // 遍历节点 HashTableTraverse(&table); // 查找节点 printf("%s\n", HashTableGet(&table, "apple")); // 释放节点 HashTableFree(&table); return 0; } ``` 这个示例程序实现了一个使用BKDRHash组织的哈希表,使用了链节点结构体和哈希表结构体,以及相关的操作函数。在遍历哈希表时,使用了两个嵌套的while循环,依次遍历哈希表的桶和桶的节点,然后输出每个节点的key和value。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值