-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTable.c
More file actions
282 lines (247 loc) · 5.2 KB
/
Copy pathhashTable.c
File metadata and controls
282 lines (247 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* linwanggm@gmail.com
* 2014.11.03
*/
#include <stdio.h>
#include <malloc.h>
#include <error.h>
#include <string.h>
#include <stdlib.h>
//数据类型
#define dataType int
#ifndef bool
typedef int bool;
#endif
#ifndef false
#define false 0
#endif
#ifndef true
#define true 1
#endif
#define mod 4
//定义节点
typedef struct NODE
{
dataType data;
struct NODE *next;
}node;
//定义hash表
typedef struct HASHTABLE
{
node *modArray[mod];
}hashTable;
//创建哈希表
hashTable *initHashTable(void)
{
hashTable *pHashTable = (hashTable *)malloc(sizeof(hashTable));
if(NULL == pHashTable)
{
perror("malloc hashTable errror.\n");
return NULL;
}
memset(pHashTable,0,sizeof(hashTable));
return pHashTable;
}
//查找数据
node *searchDataInHashTable(hashTable *pHashTable, const dataType data)
{
//判断pHashTable不为NULL,hash表已经初始化
if(NULL == pHashTable)
{
return NULL;
}
//
node *pNode;
if(NULL == (pNode=pHashTable->modArray[data%mod]))
{
return NULL;
}
while(pNode)
{
if(data==pNode->data)
{
return pNode;
}
pNode=pNode->next;
}
//pNode->next=NULL仍未查找到
return NULL;
}
//hash表插入数据
bool insertDataIntoHashTable(hashTable *pHashTable, const dataType data)
{
if(NULL == pHashTable)
{
return false;
}
node *pNode;
if(NULL == (pNode=pHashTable->modArray[data%mod]))
{
if(NULL == (pNode=(node *)malloc(sizeof(node))))
{
perror("malloc node error.\n");
return false;
}
memset(pNode,0,sizeof(node));
pNode->data=data;
pHashTable->modArray[data%mod]=pNode;
return true;
}
//若要插入数据已经在hash表中存在
if(NULL != searchDataInHashTable(pHashTable,data))
{
fprintf(stderr,"the data has already in hash table.\n");
return true;
}
//data在hash表中不存在且不为链表首部
node *tempNode;
tempNode=pHashTable->modArray[data%mod];
while(tempNode->next)
{
tempNode=tempNode->next;
}
pNode=(node *)malloc(sizeof(node));
if(NULL == pNode)
{
perror("malloc node error.");
fprintf(stderr,"malloc node error at line : %d\n", __LINE__);
return false;
}
memset(pNode,0,sizeof(node));
pNode->data=data;
pNode->next=NULL;
tempNode->next=pNode;
return true;
}
//hash表删除数据
bool deleteDataFromHashTable(hashTable *pHashTable, const dataType data)
{
if(NULL == pHashTable || NULL == pHashTable->modArray[data%mod])
{
return false;
}
//hash表中若查找不到该数据
node *pNode;
if(false == (pNode=searchDataInHashTable(pHashTable,data)))
{
return false;
}
//若pNode在首部
//if(pNode == pHashTable->modArray[data%mod])
if(pNode == pHashTable->modArray[data%mod])
{
pHashTable->modArray[data%mod]=pHashTable->modArray[data%mod]->next;
goto final;
}
//data在非首部
node *tempNode=pHashTable->modArray[data%mod];
while(pNode != tempNode->next)
{
tempNode=tempNode->next;
}
tempNode->next=pNode->next;
final:
free(pNode);
pNode=NULL;
return true;
}
//删除hash表
bool destoryHashTable(hashTable *pHashTable)
{
if(NULL == pHashTable)
return false;
int i=0;
for(i=0;i<mod;++i)
{
while(NULL != pHashTable->modArray[i])
{
dataType temp=pHashTable->modArray[i]->data;
deleteDataFromHashTable(pHashTable,temp);
}
}
return true;
}
//显示hash table内容
bool showHashTable(hashTable *pHashTable)
{
if(NULL == pHashTable)
return false;
int i=0;
for(i=0;i<mod;++i)
{
node *pNode;
pNode=pHashTable->modArray[i];
while(NULL != pNode)
{
dataType temp=pNode->data;
fprintf(stdout," %d ",temp);
pNode=pNode->next;
}
fprintf(stdout,"\n");
}
return true;
}
int main()
{
hashTable *pHashTable = initHashTable();
if (NULL == pHashTable)
{
fprintf(stderr,"create hashTable error.\n");
exit(1);
}
int intArray[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
int i=0;
//test insert
for(i=0;i<sizeof(intArray)/sizeof(int);++i)
{
if(false == (insertDataIntoHashTable(pHashTable,intArray[i])))
{
fprintf(stderr,"insert into hash table error.\n");
exit(1);
}
else
{
fprintf(stdout,"insert %d\n",intArray[i]);
}
}
//test search
node *pNode;
if(NULL == (pNode=searchDataInHashTable(pHashTable,12)))
{
fprintf(stderr,"search from hash table error.\n");
exit(1);
}
else
{
printf("search : %d\n",pNode->data);
}
//test delete
if(false == deleteDataFromHashTable(pHashTable,12))
{
fprintf(stderr,"delete from hash table error.\n");
exit(1);
}
else
{
fprintf(stdout,"delete 12\n");
}
//show hash table
if(false == showHashTable(pHashTable))
{
fprintf(stderr,"show hash table error.\n");
}
else
{
fprintf(stdout,"show hash table ok.\n");
}
//drop hash table
if(false == destoryHashTable(pHashTable))
{
fprintf(stderr,"drop hash table error.\n");
exit(1);
}
else
{
fprintf(stdout,"drop hash table ok.\n");
}
}