C语言实现哈希表(key为字符型)

 简单实现了哈希表的插入和查找功能,简要说明如下:

1、数据结构:

struct HashNode

{

            char* sKey;     //键

            int nValue;      //值

            HashNode* pNext; //当Hash值冲突时,指向HASH值相同的下一个节点。

}

HashNode* hashTable[HASH_TABLE_MAX_SIZE]; //哈希表的数组

int hash_table_size;       //哈希表中元素的个数

 

2、函数:

void hash_table_init() 初始化哈希表

void hash_table_insert(const char* skey, int nvalue) 向哈希表中插入键位skey,值为nvalue的键值对。

                            当skey已经在哈希表中时,忽略该键值对。 

void hash_table_remove(const char* skey) 从哈希表中删除键值对。

HashNode* hash_table_lookup(const char* skey) 查找键值为skey的节点。当找到时,返回对应的HashNode指针,

                            没有找到时,返回NULL。

void hash_table_release() 释放哈希表的内存空间。

C语言实现的哈希表(HashTable)源码如下:

[cpp] view plain  copy
 print ?
  1. /* 
  2.  * Author: puresky 
  3.  * Date: 2011/01/08 
  4.  * Purpose: a simple implementation of HashTable in C 
  5.  */  
  6.   
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <string.h>  
  10. #include <time.h>  
  11.   
  12. /*=================hash table start=========================================*/  
  13.   
  14. #define HASH_TABLE_MAX_SIZE 10000  
  15. typedef struct HashNode_Struct HashNode;  
  16.   
  17. struct HashNode_Struct  
  18. {  
  19.     char* sKey;  
  20.     int nValue;  
  21.     HashNode* pNext;  
  22. };  
  23.   
  24. HashNode* hashTable[HASH_TABLE_MAX_SIZE]; //hash table data strcutrue  
  25. int hash_table_size;  //the number of key-value pairs in the hash table!  
  26.   
  27. //initialize hash table  
  28. void hash_table_init()  
  29. {  
  30.     hash_table_size = 0;  
  31.     memset(hashTable, 0, sizeof(HashNode*) * HASH_TABLE_MAX_SIZE);  
  32. }  
  33.   
  34.   
  35. //string hash function  
  36. unsigned int hash_table_hash_str(const char* skey)  
  37. {  
  38.     const signed char *p = (const signed char*)skey;  
  39.     unsigned int h = *p;  
  40.     if(h)  
  41.     {  
  42.         for(p += 1; *p != '\0'; ++p)  
  43.             h = (h << 5) - h + *p;  
  44.     }  
  45.     return h;  
  46. }  
  47.   
  48. //insert key-value into hash table  
  49. void hash_table_insert(const char* skey, int nvalue)  
  50. {  
  51.     if(hash_table_size >= HASH_TABLE_MAX_SIZE)  
  52.     {  
  53.         printf("out of hash table memory!\n");  
  54.         return;  
  55.     }  
  56.   
  57.     unsigned int pos = hash_table_hash_str(skey) % HASH_TABLE_MAX_SIZE;  
  58.   
  59.     HashNode* pHead =  hashTable[pos];  
  60.     while(pHead)  
  61.     {  
  62.         if(strcmp(pHead->sKey, skey) == 0)  
  63.         {  
  64.             printf("%s already exists!\n", skey);  
  65.             return ;  
  66.         }  
  67.         pHead = pHead->pNext;  
  68.     }  
  69.   
  70.     HashNode* pNewNode = (HashNode*)malloc(sizeof(HashNode));  
  71.     memset(pNewNode, 0, sizeof(HashNode));  
  72.     pNewNode->sKey = (char*)malloc(sizeof(char) * (strlen(skey) + 1));  
  73.     strcpy(pNewNode->sKey, skey);  
  74.     pNewNode->nValue = nvalue;  
  75.   
  76.     pNewNode->pNext = hashTable[pos];  
  77.     hashTable[pos] = pNewNode;  
  78.   
  79.   
  80.     hash_table_size++;  
  81. }  
  82. //remove key-value frome the hash table  
  83. void hash_table_remove(const char* skey)  
  84. {  
  85.     unsigned int pos = hash_table_hash_str(skey) % HASH_TABLE_MAX_SIZE;  
  86.     if(hashTable[pos])  
  87.     {  
  88.         HashNode* pHead = hashTable[pos];  
  89.         HashNode* pLast = NULL;  
  90.         HashNode* pRemove = NULL;  
  91.         while(pHead)  
  92.         {  
  93.             if(strcmp(skey, pHead->sKey) == 0)  
  94.             {  
  95.                 pRemove = pHead;  
  96.                 break;  
  97.             }  
  98.             pLast = pHead;  
  99.             pHead = pHead->pNext;  
  100.         }  
  101.         if(pRemove)  
  102.         {  
  103.             if(pLast)  
  104.                 pLast->pNext = pRemove->pNext;  
  105.             else  
  106.                 hashTable[pos] = NULL;  
  107.   
  108.             free(pRemove->sKey);  
  109.             free(pRemove);  
  110.         }  
  111.     }  
  112. }  
  113.   
  114. //lookup a key in the hash table  
  115. HashNode* hash_table_lookup(const char* skey)  
  116. {  
  117.     unsigned int pos = hash_table_hash_str(skey) % HASH_TABLE_MAX_SIZE;  
  118.     if(hashTable[pos])  
  119.     {  
  120.         HashNode* pHead = hashTable[pos];  
  121.         while(pHead)  
  122.         {  
  123.             if(strcmp(skey, pHead->sKey) == 0)  
  124.                 return pHead;  
  125.             pHead = pHead->pNext;  
  126.         }  
  127.     }  
  128.     return NULL;  
  129. }  
  130.   
  131. //print the content in the hash table  
  132. void hash_table_print()  
  133. {  
  134.     printf("===========content of hash table=================\n");  
  135.     int i;  
  136.     for(i = 0; i < HASH_TABLE_MAX_SIZE; ++i)  
  137.         if(hashTable[i])  
  138.         {  
  139.             HashNode* pHead = hashTable[i];  
  140.             printf("%d=>", i);  
  141.             while(pHead)  
  142.             {  
  143.                 printf("%s:%d  ", pHead->sKey, pHead->nValue);  
  144.                 pHead = pHead->pNext;  
  145.             }  
  146.             printf("\n");  
  147.         }  
  148. }  
  149.   
  150. //free the memory of the hash table  
  151. void hash_table_release()  
  152. {  
  153.     int i;  
  154.     for(i = 0; i < HASH_TABLE_MAX_SIZE; ++i)  
  155.     {  
  156.         if(hashTable[i])  
  157.         {  
  158.             HashNode* pHead = hashTable[i];  
  159.             while(pHead)  
  160.             {  
  161.                 HashNode* pTemp = pHead;  
  162.                 pHead = pHead->pNext;  
  163.                 if(pTemp)  
  164.                 {  
  165.                     free(pTemp->sKey);  
  166.                     free(pTemp);  
  167.                 }  
  168.   
  169.             }  
  170.         }  
  171.     }  
  172. }  
  173.   
  174. /* ===============================hash table end=========================*/  
  175.   
  176.   
  177. /* ============================test function ============================*/  
  178. #define MAX_STR_LEN 20  
  179. #define MIN_STR_LEN 10  
  180. void rand_str(char r[])  
  181. {  
  182.     int i;  
  183.     int len = MIN_STR_LEN + rand() % (MAX_STR_LEN - MIN_STR_LEN);  
  184.     for(i = 0; i < len - 1; ++i)  
  185.         r[i] = 'a' + rand() % ( 'z' - 'a');  
  186.     r[len - 1] = '\0';  
  187. }  
  188.   
  189. int main(int argc, char** argv)  
  190. {  
  191.     srand(time(NULL));  
  192.     hash_table_init();  
  193.     printf("insert testing.........\n");  
  194.     int n = 10;  
  195.     const char *key1 = "aaammd";  
  196.     const char *key2 = "xzzyym";  
  197.     const char *key3 = "cdcded";  
  198.   
  199.     hash_table_insert(key1, 110);  
  200.     hash_table_insert(key2, 220);  
  201.     hash_table_insert(key3, 330);  
  202.     char str[MAX_STR_LEN + 1];  
  203.     while(n--)  
  204.     {  
  205.         rand_str(str);  
  206.         hash_table_insert(str, n);  
  207.     }  
  208.     hash_table_print();  
  209.   
  210.     printf("\nlookup testing..........\n");  
  211.     HashNode* pNode = hash_table_lookup(key1);  
  212.     printf("lookup result:%d\n", pNode->nValue);  
  213.     pNode = hash_table_lookup(key2);  
  214.     printf("lookup result:%d\n", pNode->nValue);  
  215.   
  216.     printf("\nremove testing..........\n");  
  217.     printf("before remove %s:\n", key3);  
  218.     hash_table_print();  
  219.     hash_table_remove(key3);  
  220.     printf("after remove:\n");  
  221.     hash_table_print();  
  222.     hash_table_release();  
  223.   
  224.     system("pause");  
  225.     return 0;  
  226. }  
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
哈希表是一种利用哈希函数进行快速查找的数据结构。C语言可以通过数组和指针实现哈希表。 首先需要定义一个哈希函数,将关字转换成哈希表的位置。可以使用简单的取模运算,将关字的值除以哈希表大小取余数得到哈希表位置。 例如,哈希表大小为10,关字为20,则哈希表位置为20%10=2。 接下来,定义一个结构体来表示哈希表中的每个元素,包括关字和值。 ```c struct hash_element { int key; int value; }; ``` 然后,定义一个哈希表数组,将每个元素插入到哈希表中。如果哈希表位置已经被占用,可以使用链表来解决冲突。 ```c #define HASH_SIZE 10 struct hash_element *hash_table[HASH_SIZE]; void insert(int key, int value) { int index = key % HASH_SIZE; struct hash_element *element = malloc(sizeof(struct hash_element)); element->key = key; element->value = value; if (hash_table[index] == NULL) { hash_table[index] = element; } else { struct hash_element *p = hash_table[index]; while (p->next != NULL) { p = p->next; } p->next = element; } } int search(int key) { int index = key % HASH_SIZE; struct hash_element *p = hash_table[index]; while (p != NULL) { if (p->key == key) { return p->value; } p = p->next; } return -1; } ``` 这里的insert函数将关字和值封装成一个结构体,然后根据哈希函数计算出哈希表位置。如果该位置为空,直接插入元素;否则,遍历链表直到找到空位置插入。 search函数根据哈希函数计算出哈希表位置,然后遍历链表查找关字。 以上是一个简单的哈希表实现,可以根据实际需求进行改进。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值