一步一步复习数据结构和算法基础-哈希表的链地址表示

http://blog.csdn.net/flying0033/article/details/7063608

散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。

 

下面的哈希表函数使用了(除留余数)

 

头文件:HashTable.h

[cpp]
view plain
copy

  1. #ifndef HASHTABLE_H  
  2. #define HASHTABLE_H  
  3. #define MAXSIZE 17  
  4. #define NULLKEY -32768  
  5. typedef struct node{ //关键字结点  
  6.     int data;    //关键字  
  7.     struct node *next;  //指向下一个结点  
  8. }Node;  
  9. typedef struct Table{  
  10.     Node *table;  //动态分配哈希表  
  11.     int count;    //哈希表的关键字个数  
  12. }HashTable;  
  13.   
  14. void InitHashTable(HashTable *H); //初始化哈希表  
  15. int Hash(int key);  //哈希表函数  
  16. void InsertHashTable(HashTable *H,int key); //把关键字插入哈希表  
  17. bool SearchHashTable(HashTable *H,int key); //在哈希表中查找关键字key  
  18. #endif //HASHTABLE_H  

实现文件:HashTable.cpp

[cpp]
view plain
copy

  1. #include “HashTable.h”  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. void InitHashTable(HashTable *H) //初始化哈希表  
  5. {  
  6.     H->count = MAXSIZE; //初始化哈希表的大小  
  7.     H->table = (Node *)malloc(sizeof(Node) * H->count); //创建哈希表  
  8.     for(int i = 0;i < H->count;++i) //初始化哈希表的数据  
  9.     {  
  10.         H->table[i].data = NULLKEY;  
  11.         H->table[i].next = NULL;  
  12.     }  
  13. }  
  14. int Hash(int key)  //哈希表函数  
  15. {  
  16.     return key % MAXSIZE;  
  17. }  
  18. void InsertHashTable(HashTable *H,int key) //将关键字key插入哈希表  
  19. {  
  20.     int addr = Hash(key);  
  21.     if(H->table[addr].data != key && H->table[addr].data != NULLKEY) //如果为真,创建单链表  
  22.     {  
  23.         Node *temp = (Node *)malloc(sizeof(Node));  
  24.         temp->next = H->table[addr].next;  
  25.         temp->data = key;  
  26.         H->table[addr].next = temp;  
  27.     }  
  28.     else if(H->table[addr].data == NULLKEY)  //则否直接填入  
  29.         H->table[addr].data = key;  
  30. }  
  31. bool SearchHashTable(HashTable *H,int key) //查找关键字key的值  
  32. {  
  33.     int addr = Hash(key);  
  34.     if(H->table[addr].data == key)  
  35.         return true;  
  36.     Node *p = H->table[addr].next;  
  37.     while(p != NULL) //查找单链表  
  38.     {  
  39.         if(p->data == key)  
  40.             return true;  
  41.         else  
  42.             p = p->next;  
  43.     }  
  44.     return false;  
  45. }  

测试文件:main.cpp

[cpp]
view plain
copy

  1. #include “HashTable.h”  
  2. #include <stdio.h>  
  3. int main()  
  4. {  
  5.     HashTable H;  
  6.     InitHashTable(&H);  
  7.     for(int i = 0;i < MAXSIZE * 2;i += 2)  
  8.         InsertHashTable(&H,i);  
  9.     printf(“请输入要查找的内容:\n”);  
  10.     int key;  
  11.     scanf(“%d”,&key);  
  12.     if(SearchHashTable(&H,key))  
  13.         printf(“在哈希表中找到关键字:%d\n”,key);  
  14.     else  
  15.         printf(“在哈希表中未找到关键字:%d\n”,key);  
  16.     return 0;  

    原文作者:哈希算法
    原文地址: https://blog.csdn.net/barkzhang/article/details/7776783
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞