用c实现HASH表创建、插入、查找、删除、打印

  1 /************************************************************************
  2 用c实现HASH表创建、插入、查找、删除、打印
  3 
  4 转载文章
  5 ************************************************************************/
  6 #include <stdio.h>
  7 #include <STDLIB.H>
  8 #include <MEMORY.H>
  9 
 10 #define STATUS int
 11 #define FALSE 0
 12 #define TRUE 1
 13 #define VOID void
 14 
 15 /****************************************
 16 a)定义hash表和基本数据节点 
 17 *****************************************/
 18 typedef struct _Node
 19 {
 20 int data;
 21 struct _Node *next;
 22 }NODE;
 23 
 24 typedef struct _HASH_TABLE
 25 {
 26 NODE * value[10];
 27 }HASH_TABLE;
 28 
 29 
 30 /****************************************
 31 b)创建hash表
 32 *****************************************/
 33 HASH_TABLE * create_hash_table()
 34 {
 35 HASH_TABLE* pHashTbl = (HASH_TABLE*)malloc(sizeof(HASH_TABLE));
 36 memset(pHashTbl,0,sizeof(HASH_TABLE));
 37 return pHashTbl;
 38 }
 39 
 40 
 41 /****************************************
 42 c)在hash表当中寻找数据
 43 *****************************************/
 44 NODE * find_data_in_hash(HASH_TABLE* pHashTbl,int data)
 45 {
 46 NODE* pNode;
 47 if (NULL == pHashTbl)
 48 {
 49 return NULL;
 50 }
 51 
 52 /*获得HASH表索引,为NULL则直接返回NULL*/
 53 if (NULL == (pNode = pHashTbl->value[data%10]))
 54 {
 55 return NULL;
 56 }
 57 
 58 /*在该索引下的单链表中查找节点*/
 59 while(pNode)
 60 {
 61 if ( data == pNode->data)
 62 {
 63 /*找到节点就返回当前节点*/
 64 return pNode;
 65 }
 66 /*当前节点不是,指向下一节点*/
 67 pNode = pNode->next;
 68 }
 69 
 70 /*没找到返回NULL,不过在这返回没有意义*/
 71 //return NULL;
 72 }
 73 
 74 /****************************************
 75 d)在hash表当中插入数据
 76 *****************************************/
 77 STATUS insert_data_into_hash(HASH_TABLE* pHashTbl,int data)
 78 {
 79 NODE* pNode;
 80 if (NULL == pHashTbl)
 81 {
 82 return FALSE;
 83 }
 84 
 85 if (NULL == pHashTbl->value[data%10])
 86 {
 87 pNode = (NODE*)malloc(sizeof(NODE));
 88 memset(pNode,0,sizeof(NODE));
 89 pNode->data = data;
 90 pHashTbl->value[data%10] = pNode;
 91 
 92 return TRUE;
 93 }
 94 
 95 if (NULL == find_data_in_hash(pHashTbl,data))
 96 {
 97 return FALSE;
 98 }
 99 
100 pNode = pHashTbl->value[data%10];
101 while(pNode->next)
102 {
103 pNode = pNode->next;
104 }
105 
106 pNode->next = (NODE*)malloc(sizeof(NODE));
107 memset(pNode->next,0,sizeof(NODE));
108 pNode->next->data = data;
109 
110 return TRUE;
111 }
112 
113 /****************************************
114 e)从hash表中删除数据
115 *****************************************/
116 STATUS delete_data_from_hash(HASH_TABLE* pHashTbl,int data)
117 {
118 NODE* pHead;
119 NODE* pNode;
120 
121 if (NULL == pHashTbl || NULL == pHashTbl->value[data%10])
122 {
123 return FALSE;
124 }
125 
126 if (NULL == (pNode = find_data_in_hash(pHashTbl,data)))
127 {
128 return FALSE;
129 }
130 
131 if (pNode == pHashTbl->value[data%10])
132 {
133 pHashTbl->value[data%10] = pNode->next;
134 goto final;
135 }
136 
137 pHead = pHashTbl->value[data%10];
138 
139 while(pNode != pHead->next)
140 {
141 pHead = pHead->next;
142 }
143 
144 pHead->next = pNode->next;
145 
146 final:
147 free(pNode);
148 return TRUE;
149 }
150 
151 
152 /****************************************
153 f)打印hash表中所有数据
154 如:
155 [Hash idx] [value]
156 0-------------NULL
157 1-------------1 251
158 2-------------22
159 3-------------123 43
160 4-------------NULL
161 5-------------55 15 235 525 725 275 545
162 6-------------NULL
163 7-------------257
164 8-------------NULL
165 *****************************************/
166 VOID print_hash_data(HASH_TABLE* pHashTbl)
167 {
168 NODE* pNode;
169 int i=0;
170 if (NULL == pHashTbl)
171 {
172 printf("ERROR:The hash is NULL\n");
173 }
174 
175 /*
176 if (NULL == (pNode = pHashTbl->value[10]))
177 {
178 printf("ERROR:The hash node is NULL\n");
179 }
180 */
181 printf("[Hash idx] [value]\n");
182 do 
183 { 
184 printf(" %d-------------",i);
185 if (NULL == pHashTbl->value[i])
186 {
187 i++;
188 printf("NULL\n");
189 continue;
190 }
191 
192 pNode = pHashTbl->value[i];
193 
194 while(pNode)
195 {
196 printf("%d ",pNode->data);
197 pNode = pNode->next;
198 }
199 printf("\n");
200 i++;
201 } while (i<10);
202 
203 printf("\n");
204 }
205 
206 int main()
207 {
208 HASH_TABLE* pHashTbl = create_hash_table();
209 
210 (VOID)insert_data_into_hash(pHashTbl,22);
211 (VOID)insert_data_into_hash(pHashTbl,22);
212 (VOID)insert_data_into_hash(pHashTbl,123);
213 (VOID)insert_data_into_hash(pHashTbl,436);
214 (VOID)insert_data_into_hash(pHashTbl,55);
215 (VOID)insert_data_into_hash(pHashTbl,157);
216 (VOID)insert_data_into_hash(pHashTbl,235);
217 (VOID)insert_data_into_hash(pHashTbl,256);
218 (VOID)insert_data_into_hash(pHashTbl,525);
219 (VOID)insert_data_into_hash(pHashTbl,724);
220 (VOID)insert_data_into_hash(pHashTbl,278);
221 (VOID)insert_data_into_hash(pHashTbl,209);
222 (VOID)insert_data_into_hash(pHashTbl,67);
223 (VOID)insert_data_into_hash(pHashTbl,54);
224 (VOID)insert_data_into_hash(pHashTbl,546);
225 (VOID)insert_data_into_hash(pHashTbl,350);
226 (VOID)insert_data_into_hash(pHashTbl,101);
227 (VOID)insert_data_into_hash(pHashTbl,23);
228 
229 print_hash_data(pHashTbl);
230 
231 (VOID)delete_data_from_hash(pHashTbl,55);
232 
233 print_hash_data(pHashTbl);
234 
235 
236 return 0;
237 }

 

posted @ 2013-04-23 14:53  chasu  阅读(914)  评论(0编辑  收藏  举报