码迷,mamicode.com
首页 > 其他好文 > 详细

散列表

时间:2017-09-16 15:01:16      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:table   ++   散列   dha   http   循环   search   平均情况   技术   

一、基本概念

关键字的全域为集合U,待散列的关键字集合为K,散列表的存储需求为O(K),同事针对平均情况的查找时间为O(1)。关键字k被存放在槽h(k)中,即利用散列函数h,由关键字k计算出槽的位置,h将U映射到散列表T[0...m1]的槽位上。

冲突:两个关键字可能映射到同一个槽中。

解决冲突:链接法、开放寻址法

二、通过链接法解决冲突

把散列到同一槽中的所有元素都放在一个链表中,如下图所示。

 技术分享

 

  1 /* 通过链接法解决冲突的散列表
  2    链接链表,使用双向、非循环、非排序方式
  3 */
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 
  7 #define HASH_SIZE 10
  8 #define ARRAY_SIZE 15
  9 
 10 typedef struct object {
 11     int key;
 12     struct object *prev,*next;
 13 }object_s;
 14 
 15 typedef struct chainedhash {
 16     object_s *ptable[HASH_SIZE];
 17 }chainedhash_s;
 18 
 19 //函数声明
 20 void chained_hash_init(chainedhash_s *phash);
 21 void chained_hash_insert(chainedhash_s *phash, int key);
 22 object_s *chained_hash_search(chainedhash_s *phash, int key);
 23 void chained_hash_delete(chainedhash_s *phash, object_s *pobj);
 24 void chained_hash_print(chainedhash_s *phash);
 25 int hash_function(int key);
 26 
 27 int main(void)
 28 {
 29     chainedhash_s hash;
 30     int index;    
 31         
 32     chained_hash_init(&hash);
 33     
 34     for(index=0;index<ARRAY_SIZE; index++) {
 35          chained_hash_insert(&hash, index);
 36     }
 37     
 38     chained_hash_print(&hash);
 39     return 0;
 40 }
 41 
 42 void chained_hash_init(chainedhash_s *phash)
 43 {
 44     int i;
 45     for(i=0; i<HASH_SIZE; i++)
 46         phash->ptable[i] = NULL;
 47 }
 48 
 49 void chained_hash_insert(chainedhash_s *phash, int key)
 50 {
 51     object_s *pobj;
 52     int index;
 53     
 54     pobj = (object_s*)malloc(sizeof(object_s));
 55     pobj->key = key;
 56     index = hash_function(key);
 57     
 58     // 从链表头插入
 59     if(phash->ptable[index] != NULL)
 60         phash->ptable[index]->prev = pobj;
 61     pobj->prev = NULL;
 62     pobj->next = phash->ptable[index];
 63     phash->ptable[index] = pobj;
 64     
 65     return;
 66 }
 67 
 68 object_s *chained_hash_search(chainedhash_s *phash, int key)
 69 {
 70     int index;
 71     object_s *pobj;
 72     
 73     index = hash_function(key);
 74     for(pobj=phash->ptable[index]; pobj!=NULL; pobj=pobj->next) {
 75         if(pobj->key == key)
 76             return pobj;
 77     }
 78         
 79     return NULL;
 80 }
 81 
 82 void chained_hash_delete(chainedhash_s *phash, object_s *pobj)
 83 {
 84     int index;
 85     index = hash_function(pobj->key);
 86     
 87     if(phash->ptable[index] == pobj) {
 88         phash->ptable[index] = pobj->next;
 89         if(pobj->next != NULL)
 90             pobj->next->prev = NULL;
 91     } else {
 92         pobj->prev->next = pobj->next;
 93         if(pobj->next != NULL)
 94             pobj->next->prev = pobj->prev;
 95     }
 96     free(pobj);
 97 }
 98 
 99 void chained_hash_print(chainedhash_s *phash)
100 {
101     int index;
102     object_s *pobj;
103     
104     for(index=0; index<HASH_SIZE; index++) {
105         printf("%d:",index);
106         for(pobj=phash->ptable[index]; pobj!=NULL; pobj=pobj->next) {
107             printf("%d ",pobj->key);
108         }
109         printf("\n");
110     }
111 }
112 
113 int hash_function(int key)
114 {
115     return key%10;
116 }

 

散列表

标签:table   ++   散列   dha   http   循环   search   平均情况   技术   

原文地址:http://www.cnblogs.com/yanxin880526/p/7530891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!