散列(开发地址法线性探测法)

散列表是散列函数的一个主要应用,使用散列表能够快速的按照关键字查找数据记录。

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #define idle_len 10
5
6 /*********************************************************************
7 * 文件名称: // idlecolumn.cpp
8 * 文件标识: //
9 * 内容摘要: // 散列(开发地址法-线性探测法)
10 * 其它说明: //
11 * 当前版本: // v1.0
12 * 作 者: // pipi
13 * 完成日期: // 2011年7月13日
14 **********************************************************************/
15
16 typedef struct hashnode
17 {
18 int data; //初始值为0
19 int state; //0 未使用 1 使用
20 }hashnode_st;
21
22
23 int main()
24 {
25 hashnode_st a[10];
26
27 int i =0,j=0;
28
29 for (int k=0;k<idle_len;k++)
30 {
31 a[k].data = 0;
32 a[k].state = 0;
33 }
34
35 int order[] = {21,34,56,78,91,23,45,10};
36
37 while(1)
38 {
39 if(1==a[order[i]%10].state) //当前节点被使用
40 {
41 int k = 0;
42 while(1==a[order[i]%10+(++k)].state);
43 a[order[i]%10+k].data = order[i];
44 }
45
46
47 if(0==a[order[i]%10].state) //当前节点未使用
48 {
49 a[order[i]%10].data = order[i];
50 a[order[i]%10].state = 1;
51 }
52
53 i++;
54 if(i==sizeof(order)/sizeof(int))
55 {
56 break;
57 }
58
59
60 }
61 for( i=0;i<idle_len;i++)
62 {
63
64 printf("%d ",a[i].data);
65 }
66 return 0;
67
68 }
69
70
71 //10 21 91 23 34 45 56 0 78 0



原文地址:https://www.cnblogs.com/pipicfan/p/2283696.html