首页 > 其他 > 详细

写给自己看的散列表(1):分离链接法

时间:2019-05-01 19:42:33      阅读:143      评论:0      收藏:0      [点我收藏+]

搬运自我的CSDN https://blog.csdn.net/u013213111/article/details/88830154

1.定义
分离链接法实际上是通过在散列表的某个位置上用单链表存储一串数据(这些数据通过散列函数得到的地址相同)来解决冲突的问题,所以在散列表的结构中需要有一个数组(也可以说是指针)pLnode *pList去存储这些链表的“表头”节点。而在链表数据结构中,节点实际上是一个“链表结构”类型的指针(比如在之前单链表的代码中头结点的定义就是Lnode *head),这个指针指向一片内存,这片内存里存储了节点的数据和指向下一个节点的指针。这就导致散列表结构中的pList是一个“二级指针”,在后续新建散列表和插入元素的函数中一定要注意指针是否用得正确。

 1 typedef struct node
 2 {
 3     eletype data;
 4     struct node *next;
 5 }Lnode;
 6 
 7 typedef Lnode * pLnode;
 8 
 9 typedef struct table
10 {
11     int size;
12     pLnode *pList;
13 }Htable

2.新建散列表
step 1:为散列表结构分配内存。
step 2:计算合适的散列表大小,通常的原则是取素数,也就是这里用的NextPrime函数(这个函数是从DSAA in C的源码中copy的),至于为什么取素数,参考知乎Hash时取模一定要模质数吗?。
step 3:为散列表结构中的二级指针pList(也就是存储所有链表表头的数组)中的每个元素(即每个表头,是一级指针)分配所指向的内存。

 1 Htable *CreateHashTable(int tsize)
 2 {
 3     Htable *H;
 4 
 5     if (tsize < MinSize) {
 6         printf("Table size must >= %d\n", MinSize);
 7         return NULL;
 8     }
 9 
10     H = malloc(sizeof(Htable));
11     if (H == NULL) {
12         printf("Create table failed\n");
13         return NULL;
14     }
15     
16     H->size = NextPrime(tsize);
17     H->pList = malloc(sizeof(pLnode) * H->size);
18     
19     //I prefer to use pointer rather than using array
20     //actually *H->pList is the head of a single-linked list, there are "size" single-linked lists
21     pLnode *pLhead; 
22     pLhead = H->pList; 
23     for (int i = 0; i < H->size; i++) {        
24         *pLhead = malloc(sizeof(Lnode));
25         if (*pLhead == NULL) {
26             printf("Out of space\n");
27             return NULL;
28         }
29         (*pLhead)->next = NULL;
30         pLhead++; //!!!!!go to head of next single-linked list 
31     }
32     
33     //using array
34     /*
35     for (int i = 0; i < H->size; i++) {
36         H->pList[i] = malloc(sizeof(Lnode));
37         if (H->pList[i] == NULL) {
38             printf("Out of space\n");
39             return NULL;
40         }
41         H->pList[i]->next = NULL;
42     }
43     */
44     return H;
45 }

3.散列函数
用于key对应的散列表地址,返回值是地址也就是unsigned int。

1 uint Hash(Htable *H, const char *key) //const eletype key != const char *key
2 {
3     uint hashval = 0;
4     while (*key != \0)
5         hashval = (hashval << 5) + *key++;
6     return hashval % H->size;
7 }

4.查找
返回值是指向单链表中某个节点的指针(Lnode *)。

1 Lnode *Find(Htable *H, eletype key)
2 {
3     Lnode *Lhead, *current;
4     Lhead = H->pList[Hash(H, key)];
5     current = Lhead->next; //Lhead->next is NULL, initialized in CreateHashTable()
6     while (current != NULL && strcmp(current->data, key) != 0) //eletype is string, use strcmp!
7         current = current->next;
8     return current;
9 }

5.插入
注意用的是从头插入而不是从尾插入,即新节点插入在头结点和旧节点之间,成为新的head->next。

 1 void Insert(Htable *H, eletype key)
 2 {
 3     Lnode *pos, *Lhead, *s;
 4     pos = Find(H, key);
 5     if (pos == NULL) {
 6         s = malloc(sizeof(Lnode));
 7         if (s == NULL) {
 8             printf("Out of space\n");
 9             return;
10         }
11         Lhead = H->pList[Hash(H, key)];
12         s->next = Lhead->next; //insert new node from head, old nodes move backward
13         s->data = key; //should use strcpy, however strcpy is unsafe
14         Lhead->next = s;
15     }
16 }

 

写给自己看的散列表(1):分离链接法

原文:https://www.cnblogs.com/lyrich/p/10800456.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!