-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.c
275 lines (188 loc) · 5.37 KB
/
hashtable.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
#include <stdio.h>
#include "hashtable.h"
void free_hashtable(Hashtable *hashtable);
Hashtable *create_hashtable(Hashtable *hashtable, int size)
{
int i = 0;
hashtable = malloc(sizeof(Hashtable));
if (hashtable == NULL) {
fprintf(stderr, "Cannot allocate memory");
exit(-1);
}
hashtable->size = size;
hashtable->buckets = malloc(size * sizeof(Bucket));
if(hashtable->buckets == NULL) {
fprintf(stderr, "Cannot allocate memory");
exit(-1);
}
for (i = 0; i < size; i++)
hashtable->buckets[i].bucket_head = NULL;
return hashtable;
}
Hashtable *resize_hashtable(Hashtable *hashtable, int size)
{
int i = 0;
Node *current_node;
Hashtable *new_hashtable = NULL;
new_hashtable = create_hashtable(new_hashtable, size);
/* copy the old hash into the new one */
for (i = 0; i < hashtable->size; i++) {
current_node = hashtable->buckets[i].bucket_head;
while (current_node != NULL) {
add_word(new_hashtable, current_node->word);
current_node = current_node->next_node;
}
}
free_hashtable(hashtable);
return new_hashtable;
}
Hashtable *make_double(Hashtable *hashtable)
{
return resize_hashtable(hashtable, hashtable->size * 2);
}
Hashtable *make_half(Hashtable *hashtable)
{
return resize_hashtable(hashtable, (hashtable->size) / 2);
}
int print_bucket(Hashtable *hashtable, int bucket_index, FILE *out_file)
{
int return_code = 0;
Node *current_node;
/* check for bucket_index consistency */
if (bucket_index > hashtable->size || bucket_index < 0) {
return_code = -1;
fprintf(stderr, "The bucket index is invalid");
} else { /* iterate over the selected bucket */
current_node = hashtable->buckets[bucket_index].bucket_head;
while (current_node != NULL) {
if (current_node->next_node == NULL) {
fprintf(out_file, "%s", current_node->word);
fprintf(out_file, "\n");
} else {
fprintf(out_file, "%s ", current_node->word);
}
current_node = current_node->next_node;
}
}
return return_code;
}
int print_hashtable(Hashtable *hashtable, FILE *out_file)
{
int i = 0, return_code = 0;
for (i = 0; i < hashtable->size; i++)
return_code = print_bucket(hashtable, i, out_file);
return return_code;
}
int find_word(Hashtable *hashtable, char *word, FILE *out_file)
{
int position = hash(word, hashtable->size), return_code = 0;
Node *current_node = hashtable->buckets[position].bucket_head;
while (current_node != NULL) {
if (strcmp(current_node->word, word) == 0) {
fprintf(out_file, "%s\n", "True");
return return_code;
}
current_node = current_node->next_node;
}
fprintf(out_file, "%s\n", "False");
return return_code;
}
void clear_hashtable(Hashtable *hashtable)
{
int i = 0;
Node *back_node;
Node *head_node;
/* iterate over the buckets and free all nodes */
for (i = 0; i < hashtable->size; i++) {
head_node = hashtable->buckets[i].bucket_head;
while (head_node != NULL) {
back_node = head_node;
head_node = head_node->next_node;
free(back_node->word);
free(back_node);
}
hashtable->buckets[i].bucket_head = NULL;
}
}
void free_hashtable(Hashtable *hashtable)
{
clear_hashtable(hashtable);
hashtable->size = 0;
free(hashtable->buckets);
free(hashtable);
}
/* creates a new node with the specified word */
Node *get_new_node(char *word)
{
Node *new_node;
new_node = malloc(sizeof(Node));
if (new_node == NULL) {
fprintf(stderr, "Cannot allocate memory");
exit(-1);
}
new_node->next_node = NULL;
new_node->word = malloc((strlen(word) + 1) * sizeof(char));
if (new_node->word == NULL) {
fprintf(stderr, "Cannot allocate memory");
exit(-1);
}
strcpy(new_node->word, word);
return new_node;
}
int add_word(Hashtable *hashtable, char *word)
{
int return_code = 0;
int position = hash(word, hashtable->size);
Node *new_node = NULL;
Node *current_node = NULL;
/* if it's the first word in the bucket */
if (hashtable->buckets[position].bucket_head == NULL) {
new_node = get_new_node(word);
hashtable->buckets[position].bucket_head = new_node;
}
else {
current_node = hashtable->buckets[position].bucket_head;
/* iterate until the last node is reached */
while (current_node->next_node != NULL) {
/* if the word is already in the hash */
if (strcmp(current_node->word, word) == 0)
break;
current_node = current_node->next_node;
}
/* if the last node doesn't contain the word */
if (strcmp(current_node->word, word) != 0) {
new_node = get_new_node(word);
current_node->next_node = new_node;
}
}
return return_code;
}
int remove_word(Hashtable *Hash, char *word)
{
int pos = hash(word, Hash->size), return_code = 0;
Node *current_node = Hash->buckets[pos].bucket_head;
/* back_node keeps is always behind the current_node */
Node *back_node = current_node;
/* temp_node is used to deallocate memory */
Node *temp_node;
/* loop until we reach the word or NULL */
while (current_node != NULL && strcmp(current_node->word, word) != 0) {
back_node = current_node;
current_node = current_node->next_node;
}
if (current_node != NULL) {
/* if it's the first node in the bucket */
if (back_node == current_node) {
temp_node = Hash->buckets[pos].bucket_head;
Hash->buckets[pos].bucket_head = back_node->next_node;
free(temp_node->word);
free(temp_node);
} else {
back_node->next_node = current_node->next_node;
free(current_node->word);
free(current_node);
return_code = 0;
}
}
return return_code;
}