-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
301 lines (270 loc) · 5.32 KB
/
hash.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
#define TABLESIZE 27
struct linknode
{
char fileName[SIZE];
struct linknode *next;
};
void initializeHashTable(struct linknode **hashTable)
{
int i;
for(i=0;i<TABLESIZE;i++)
hashTable[i] = NULL;
}
int calcHash(char alpha)
{
int asciiVal, hash;
asciiVal = alpha;
int a = 'a';
int z = 'z';
int A = 'A';
int Z = 'Z';
if(asciiVal >= a && asciiVal <= z)
hash = asciiVal - a + 1;
else if(asciiVal >= A && asciiVal <= Z)
hash = asciiVal - A + 1;
else
hash = 0;
return (hash);
}
int getSize(struct linknode *hashNode)
{
int count = 0;
struct linknode *tmp;
tmp = hashNode;
while (tmp != NULL)
{
count++;
tmp = tmp->next;
}
return count;
}
struct linknode* insertNode(struct linknode *hashNode, char name[])
{
int i;
struct linknode *tmp;
tmp = (struct linknode *) malloc(sizeof(struct linknode));
for(i=0; i < strlen(name); i++)
tmp -> fileName[i] = name[i];
tmp -> next = hashNode;
hashNode = tmp;
return hashNode;
}
struct linknode* deleteNode(struct linknode *hashNode, char name[])
{
int i;
struct linknode *tmp, *pre;
tmp = hashNode;
short int flag = 0;
pre = NULL;
if(tmp == NULL)
return NULL;
else if(strcmp(name, tmp->fileName) == 0)
{
hashNode = tmp->next;
free(tmp);
return hashNode;
}
else
{
while(tmp != NULL && flag == 0)
{
if (strcmp(name, tmp->fileName) == 0)
{
pre->next = tmp->next;
free(tmp);
flag = 1;
}
pre = tmp;
tmp = tmp->next;
}
return hashNode;
}
}
struct linknode** searchNode(struct linknode *hashNode, char name[])
{
struct linknode **item, *tmp;
int i, x=0, count=0;
tmp = hashNode;
char namestr[strlen(name)];
while(tmp != NULL)
{
for(i=0; name[i] != '\0'; i++)
namestr[i] = tmp->fileName[i];
namestr[i] = '\0';
if(strcmp(name, namestr) == 0)
count++;
tmp = tmp->next;
}
item = (struct linknode **) malloc(sizeof(struct linknode) * (count+1)); //dereference item first before dereferencing *item in the next line
*item = (struct linknode *) malloc(sizeof(struct linknode) * (count+1));
tmp = hashNode;
while(tmp != NULL)
{
for(i=0; name[i] != '\0'; i++)
namestr[i] = tmp->fileName[i];
namestr[i] = '\0';
if(strcmp(name, namestr) == 0)
item[x++] = tmp;
tmp = tmp->next;
}
item[x] = NULL;
return (item);
}
void showList(struct linknode *hashNode)
{
struct linknode *tmp;
tmp = hashNode;
if (tmp == NULL)
printf("Empty hashNode.");
else
while(tmp != NULL)
{
printf(" %s ", tmp->fileName);
tmp = tmp->next;
}
}
void insertFile(char name[], struct linknode **hashTable)
{
int hash;
char init;
init = name[0];
hash = calcHash(init);
hashTable[hash] = insertNode(hashTable[hash], name);
}
void deleteFile(char name[], struct linknode **hashTable)
{
int hash;
char init;
init = name[0];
hash = calcHash(init);
hashTable[hash] = deleteNode(hashTable[hash], name);
}
void searchFile(char name[], struct linknode **hashTable)
{
int hash, size, i=0, flag = 0;
char init;
struct linknode **temp;
init = name[0];
hash = calcHash(init);
temp = searchNode(hashTable[hash], name);
while(temp[i] != NULL)
{
printf("\nFile found: %s\n", temp[i] -> fileName);
i++;
flag = 1;
}
if (flag == 0)
printf("\nFile not found!\n");
}
void displayTable(struct linknode **hashTable)
{
int i;
for(i=0; i < TABLESIZE; i++)
{
printf("%d: ", i);
showList(hashTable[i]);
printf("\n");
}
}
void writeToDisk(struct linknode **hashTable)
{
FILE *writefiles;
int i, count = 0;
struct linknode *temp;
char *file;
writefiles = fopen("./hashtable.bin", "wb");
if(!writefiles)
{
printf("Error: Unable to open file..");
}
fseek(writefiles, sizeof(int), SEEK_CUR);
for(i=0; i < TABLESIZE; i++)
{
temp = hashTable[i];
while(temp != NULL)
{
file = temp->fileName;
fwrite(file, SIZE, 1, writefiles);
temp = temp->next;
count++;
}
}
rewind(writefiles);
fwrite(&count, sizeof(int), 1, writefiles);
fclose(writefiles);
}
void readFromDisk(struct linknode **hashTable)
{
FILE *readfiles;
int i = 0, count = 0;
struct linknode *temp;
char file[SIZE];
readfiles = fopen("./hashtable.bin", "rb");
if(!readfiles)
{
printf("Error: Unable to open file..");
}
fread(&count, sizeof(int), 1, readfiles);
while(i != count)
{
fread(file, SIZE, 1, readfiles);
insertFile(file, hashTable);
i++;
}
fclose(readfiles);
}
int main()
{
int choice;
char ans = 'y';
char fname[SIZE];
struct linknode *hashTable[TABLESIZE];
initializeHashTable(hashTable);
while(ans == 'y')
{
printf("\n1. Create file\n");
printf("2. Delete File\n");
printf("3. Search File\n");
printf("4. Display Table\n");
printf("5. Exit\n");
printf("6. Write to disk\n");
printf("7. Read from disk\n");
printf("\nEnter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter file name: ");
scanf("%s", fname);
insertFile(fname, hashTable);
break;
case 2:
printf("Enter file name: ");
scanf("%s", fname);
deleteFile(fname, hashTable);
break;
case 3:
printf("Enter file name: ");
scanf("%s", fname);
searchFile(fname, hashTable);
break;
case 4:
displayTable(hashTable);
break;
case 5:
exit(0);
case 6:
writeToDisk(hashTable);
break;
case 7:
readFromDisk(hashTable);
break;
}
}
return (0);
}