/
myhash.c
238 lines (195 loc) · 4.63 KB
/
myhash.c
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
/*************************************************************************
> File Name: myhash.c
> Author:
> Mail:
> Created Time: 2015年11月10日 星期二 19时34分43秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include"myhash.h"
#define MinTableSize 10
struct linknode{
int data;
Position nextPtr;
};
struct hashtbl{
int TableSize;
List *TheList;
};
/*NextPrime*/
static int NextPrime(int N){
int i;
if(N % 2 == 0){
N++;
}
for(;;N += 2){
for(i = 3;i * i <= N;i += 2){
if(N % i == 0){
goto ContOuter;
}
return N;
}
ContOuter: ;
}
}
/*Hash*/
int Hash(int Key,int TableSize){
return Key % TableSize;
}
int main(void){
HashTable H = InitializeTable(20);
printf("%d\n",H->TableSize);
printf("%d\n",H->TheList[0]->data);
int choice;
int item;
instructions();
printf("your choice:");
scanf("%d",&choice);
while(choice != 4){
switch(choice){
case 1:
printf("\nEnter a number:\n");
scanf("%d",&item);
Insert(item,H);
break;
case 2:
printf("\nEnter a number to delete\n");
scanf("%d",&item);
Delete(item,H);
break;
case 3:
printTable(H);
break;
default:
printf("\nInvaild choice\n\n");
instructions();
break;
}
printf("\nyour choice:");
scanf("%d",&choice);
}
DestroyTable(H);
printf("End of run\n");
return 0;
}
/*instructions*/
void instructions(void){
printf("Enter your choice:\n"
"1 to insert an element into Hash\n"
"2 to delete an element\n"
"3 to see list from Hash\n"
"4 to end\n");
}
/*InitializeTable分离链接散列表的初始化*/
HashTable InitializeTable(int TableSize){
HashTable H;
int i;
if(TableSize < MinTableSize){
printf("Table is too small\n");
return NULL;
}
H = malloc(sizeof(HashTbl));
if(H != NULL){
H->TableSize = NextPrime(TableSize);
H->TheList = malloc(sizeof(List) * H->TableSize);
if(H->TheList == NULL){
printf("Out of spzce!!!\n");
}
for(i = 0;i < H->TableSize;i++){
H->TheList[i] = malloc(sizeof(LinkNode));
if(H->TheList[i] == NULL){
printf("Out of space!!!\n");
}else{
H->TheList[i]->nextPtr = NULL;
}
}
}else{
printf("Out of space!!!\n");
}
return H;
}
/*Find*/
Position Find(int Key,HashTable H){
List L;
Position P;
L = H->TheList[Hash(Key,H->TableSize)];
P = L->nextPtr;
while(P != NULL && P->data != Key){
P = P->nextPtr;
}
return P;
}
/*Insert*/
void Insert(int Key,HashTable H){
Position tempPtr,Pos;
List L;
Pos = Find(Key,H);
if(Pos == NULL){
tempPtr = malloc(sizeof(LinkNode));
if(tempPtr != NULL){
L = H->TheList[Hash(Key,H->TableSize)];
tempPtr->data = Key;
tempPtr->nextPtr = L->nextPtr;
L->nextPtr = tempPtr;
}else{
printf("Out of space!!!\n");
}
}
}
/*Retrieve*/
int Retrieve(Position P){
return P->data;
}
/*DestroyTable*/
void DestroyTable(HashTable H){
int i;
for(i = 0;i < H->TableSize;i++){
Position P = H->TheList[i];
Position Temp;
while(P != NULL){
Temp = P->nextPtr;
free(P);
P = Temp;
}
}
free(H->TheList);
free(H);
}
/*Delete*/
int Delete(int Key,HashTable H){
Position P,temp;
if((temp = Find(Key,H)) == NULL){
printf("Can't find\n");
return 0;
}
P = H->TheList[Hash(Key,H->TableSize)]->nextPtr;
if(P->data == Key){
H->TheList[Hash(Key,H->TableSize)]->nextPtr = P->nextPtr;
free(P);
return Key;
}
if(P->nextPtr == NULL){
H->TheList[Hash(Key,H->TableSize)]->nextPtr = NULL;
free(P);
return Key;
}
while(P != NULL && P->nextPtr->data != Key){
P = P->nextPtr;
}
P->nextPtr = temp->nextPtr;
free(temp);
return Key;
}
/*printTable*/
void printTable(HashTable H){
int i;
Position P;
for(i = 0;i < H->TableSize;i++){
P = H->TheList[i]->nextPtr;
while(P != NULL){
printf("%d -->",P->data);
P = P->nextPtr;
}
printf("NULL\n");
}
}