散列表(C版)

可以直接编译通过,可以试一下….

  
   
    1 
   #include 
   <
   stdio.h
   >
   
2 #include < stdlib.h >
3 #include < string .h >
4 #include < ctype.h >
5
6
7   #define N 30
8   #define L 38
9 #define P 37
10
11
12 typedef struct
13 {
14 char * name;
15 char * py;
16 int k;
17 }old;
18 old oldlist[N];
19
20 typedef struct
21 {
22 char * name;
23 char * py;
24 int k;
25 int b;
26 }hx;
27 hx hlist[L];
28
29 void InputOldlist()
30 {
31 int i,s = 0 ,r;
32 /* for(i=1;i<=N;i++){
33 oldlist[i-1].name=(char*)malloc(sizeof(char)*20);
34 printf("请输入人名:");
35 gets(oldlist[i-1].name);
36 oldlist[i-1].py=(char*)malloc(sizeof(char)*20);
37 printf("请输入拼音:");
38 gets(oldlist[i-1].py);
39 } */
40 oldlist[ 0 ].name = " 栾雪峰 " ; oldlist[ 0 ].py = " luan xue feng " ;
41 oldlist[ 1 ].name = " 刘翔 " ; oldlist[ 1 ].py = " liu xiang " ;
42 oldlist[ 2 ].name = " 马三立 " ; oldlist[ 2 ].py = " ma san li " ;
43 oldlist[ 3 ].name = " 李开复 " ; oldlist[ 3 ].py = " li kai fu " ;
44 oldlist[ 4 ].name = " 李彦宏 " ; oldlist[ 4 ].py = " li yan hong " ;
45 oldlist[ 5 ].name = " 王冰杰 " ; oldlist[ 5 ].py = " wang bin jie " ;
46 oldlist[ 6 ].name = " 孟茂昌 " ; oldlist[ 6 ].py = " meng mao chang " ;
47 oldlist[ 7 ].name = " 蒋睿杰 " ; oldlist[ 7 ].py = " jiang rui jie " ;
48 oldlist[ 8 ].name = " 嫦娥 " ; oldlist[ 8 ].py = " chang er " ;
49 oldlist[ 9 ].name = " 孙悟空 " ; oldlist[ 9 ].py = " sun wu kong " ;
50 oldlist[ 10 ].name = " 猪八戒 " ; oldlist[ 10 ].py = " zhu ba jie " ;
51 oldlist[ 11 ].name = " 徐琛 " ; oldlist[ 11 ].py = " xu chen " ;
52 oldlist[ 12 ].name = " 唐僧 " ; oldlist[ 12 ].py = " tang seng " ;
53 oldlist[ 13 ].name = " 周星驰 " ; oldlist[ 13 ].py = " zhou xing chi " ;
54 oldlist[ 14 ].name = " 董寸瑞 " ; oldlist[ 14 ].py = " dong cun rui " ;
55 oldlist[ 15 ].name = " 黄继光 " ; oldlist[ 15 ].py = " huang ji guang " ;
56 oldlist[ 16 ].name = " 金庸 " ; oldlist[ 16 ].py = " jin rong " ;
57 oldlist[ 17 ].name = " 杨过 " ; oldlist[ 17 ].py = " yang guo " ;
58 oldlist[ 18 ].name = " 小龙女 " ; oldlist[ 18 ].py = " xiao long nv " ;
59 oldlist[ 19 ].name = " 郭大侠 " ; oldlist[ 19 ].py = " guo da xia " ;
60 oldlist[ 20 ].name = " 黄蓉 " ; oldlist[ 20 ].py = " huang rong " ;
61 oldlist[ 21 ].name = " 老师 " ; oldlist[ 21 ].py = " lao shi " ;
62 oldlist[ 22 ].name = " 学生 " ; oldlist[ 22 ].py = " xue sheng " ;
63 oldlist[ 23 ].name = " 小平同志 " ; oldlist[ 23 ].py = " xiao ping tong zhi " ;
64 oldlist[ 24 ].name = " 泽民 " ; oldlist[ 24 ].py = " ze min " ;
65 oldlist[ 25 ].name = " 圣诞老人 " ; oldlist[ 25 ].py = " sheng dan lao ren " ;
66 oldlist[ 26 ].name = " 朱德庸 " ; oldlist[ 26 ].py = " zhu de yong " ;
67 oldlist[ 27 ].name = " 阿扁 " ; oldlist[ 27 ].py = " a bian " ;
68 oldlist[ 28 ].name = " 连战 " ; oldlist[ 28 ].py = " lian zhan " ;
69 oldlist[ 29 ].name = " 幸福 " ; oldlist[ 29 ].py = " xing fu " ;
70
71 for (i = 0 ;i < N;i ++ )
72 {
73 for (s = 0 ,r = 0 ;oldlist[i].py[r] != ' \0 ' ;r ++ )
74 {
75 s = toascii(oldlist[i].py[r]) + s;
76 }
77 oldlist[i].k = s;
78 }
79 }
80
81 void hash()
82 {
83 int i,adr,d;
84 float average,sum = 0 ;
85 for (i = 0 ;i < L;i ++ )
86 {
87 hlist[i].name = "" ;
88 hlist[i].py = "" ;
89 hlist[i].k = 0 ;
90 hlist[i].b = 0 ;
91 }
92
93 for (i = 0 ;i < N;i ++ )
94 {
95 adr = (oldlist[i].k) % P;
96 d = adr;
97 if (hlist[adr].b == 0 )
98 {
99 hlist[adr].k = oldlist[i].k;
100 hlist[adr].name = oldlist[i].name;
101 hlist[adr].py = oldlist[i].py;
102 hlist[adr].b = 1 ;
103 sum = sum + 1 ;
104 }
105 else
106 {
107 do
108 {
109 d = (d + adr % 10 + 1 ) % P;
110 sum = sum + 1 ;
111 } while (hlist[d].b != 0 );
112 hlist[d].k = oldlist[i].k;
113 hlist[d].name = oldlist[i].name;
114 hlist[d].py = oldlist[i].py;
115 hlist[d].b = 1 ;
116 }
117 }
118 average = sum / 30 ;
119 printf( " 平均查找长度为:%f\n " ,average);
120 if (average > 2 )
121 hash();
122 }
123
124
125 void findlist()
126 {
127 char c[ 20 ];
128 int i = 0 ,d,g,adr,s = 0 ;
129 printf( " 请输入你要查找的人的拼音:\n " );
130 getchar();
131 gets(c);
132
133 for (i = 0 ;c[i] != ' \0 ' ;i ++ )
134 {
135 s = toascii(c[i]) + s;
136 }
137 adr = s % P;
138 d = adr;
139 if (strcmp(hlist[adr].py,c) == 0 )
140 {
141 printf( " 姓名:%s 拼音:%s 哈希表中存储位置:%d\n " ,hlist[adr].name,hlist[adr].py,adr);
142 }
143 else if (hlist[adr].b == 0 )
144 {
145 printf( " 哈希表中无记录!\n " );
146 }
147 else
148 {
149 g = 0 ;
150 for (i = 0 ;g == 0 ;i ++ )
151 {
152 d = (d + adr % 10 + 1 ) % P;
153 if (hlist[d].b == 0 )
154 {
155 printf( " 哈希表中无记录!\n " );
156 g = 1 ;
157 }
158 if (strcmp(hlist[d].py,c) == 0 )
159 {
160 printf( " 姓名:%s 拼音:%s 哈希表中存储位置:%d\n " ,hlist[d].name,hlist[d].py,d);
161 g = 1 ;
162 }
163 }
164 }
165 }
166
167 void printlist()
168 {
169 int i;
170 for (i = 0 ;i < L;i ++ )
171 printf( " %s %s %d \n " ,hlist[i].name,hlist[i].py,hlist[i].k);
172 }
173
174 int main()
175 {
176 int ch;
177 InputOldlist();
178 hash();
179 do
180 {
181 printf( " 1.查找 2.输出哈希表 3.退出程序\n " );
182 scanf( " %d " , & ch);
183 switch (ch)
184 {
185 case 1 :
186 findlist();
187 printf( " Press any key to continue.\n " );
188 break ;
189 case 2 :
190 printlist();
191 printf( " Press any key to continue.\n " );
192 break ;
193 case 3 :
194 break ;
195 default :
196 printf( " No this operation.\n " );
197 printf( " Press any key to continue.\n " );
198 }
199 getchar();
200 } while (ch != 3 );
201 }

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/hicjiajia/archive/2010/12/07/1898482.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞