/
hashtable.c
526 lines (401 loc) · 11.8 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* hashtable.c
*
* Created on: May 15, 2016
* Author: abdelrahman
*/
/*
* includes structures and functions for
* Variable
* List
* HashTable
*/
/*********************************************************************************************/
/*************************************** INCLUDES ********************************************/
/*********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
/*********************************************************************************************/
/********************************* STRUCTURE DEFINITIONS *************************************/
/*********************************************************************************************/
// structure of a variable
typedef struct Variable {
char name[15];
double value;
} Variable;
// structure of a node variable
typedef struct Node {
Variable variable;
struct Node* next;
} Node;
// structure of a list
typedef struct List {
Node* head;
Node* tail;
} List;
#define HTSIZE 26
typedef struct HashTable {
List* table[HTSIZE];
} HashTable;
/***************************************************************************************/
/****************************** FUNCTION PROTOTYPES ************************************/
/***************************************************************************************/
// fuctions for Variable
char* VariableToString(Node* n);
// fuctions for Node
Node* newNode(char* name, double value);
char* getName(Node* n);
double getValue(Node* n);
void setName(Node* n, char* value);
// functions for List
void initList(List* list);
void addHead(List* list, char* name, double value);
void addTail(List* list, char* name, double value);
void deleteHead(List* list);
void deleteTail(List* list);
Node* searchList(List* l, char* name);
int isEmpty(List* list);
char* listToString(List* list);
// functions for hash table
void initHashTable(HashTable* t);
int Hash(char* variableName);
void addToHash(HashTable* t, char* name, double value);
void addVariableToHash(HashTable* t, Variable v);
double getValueForName(HashTable* t, char* name);
char* tableToString(HashTable* t);
/*********************************************************************************************/
/******************************* FUNCTIONS IMPLEMENTATION ************************************/
/************************************ NODE FUNCTIONS *****************************************/
/*********************************************************************************************/
// create a new node with a given variable name and value
Node* newNode(char* name, double value) {
Node* n = (Node*) malloc(sizeof(Node));
strcpy(n->variable.name, name);
n->variable.value = value;
n->next = NULL;
return n;
}
/***********************************************************/
char* getName(Node* n) {
if (n == NULL)
return NULL;
return n->variable.name;
}
/***********************************************************/
double getValue(Node* n) {
if (n == NULL)
return INT_MIN;
return n->variable.value;
}
/***********************************************************/
void setName(Node* n, char* name) {
if (n == NULL || name == NULL) {
return;
}
// else
strcpy(n->variable.name, name);
}
/***********************************************************/
void setValue(Node* n, double value) {
n->variable.value = value;
}
/*********************************************************************************************/
/*********************************** VARIABLE FUCTIONS ***************************************/
/*********************************************************************************************/
char* VariableToString(Node* n) {
if (n == NULL)
return NULL;
char* str = (char*) malloc(10 * sizeof(char));
str[0] = '\0';
sprintf(str, "%s = %.2lf", n->variable.name, n->variable.value);
return str;
}
/*********************************************************************************************/
/************************************* LIST FUNCTIONS ****************************************/
/*********************************************************************************************/
// initialize the linked list
void initList(List* list) {
list->head = NULL;
list->tail = NULL;
}
int getListSize(List* list) {
if (list == NULL)
return 0;
Node* traversal = list->head;
int count = 0;
while (traversal != NULL) {
count++;
traversal = traversal->next;
}
return count;
}
/***********************************************************/
/* checks if a given node exits in a given list
* returns 1 if it exists
* returns 0 if it doesnt
*/
int NodeExistsInList(List* list, Node* n) {
if (list->head == NULL || n == NULL)
return 0;
// traversal pointer
Node* trav = list->head;
// loop the list looking for the node
while (trav != NULL) {
if (trav == n)
return 1;
trav = trav->next;
}
// node doesnt exist in list
return 0;
}
/***********************************************************/
void addHead(List* list, char* name, double value) {
Node* node = newNode(name, value);
node->next = list->head;
list->head = node;
// if the list was originally empty
// then set the tail to point at the node just created
if (list->tail == NULL) {
list->tail = node;
}
}
/***********************************************************/
void addTail(List* list, char* name, double value) {
Node* node = newNode(name, value);
// special case: first node in list
if (list->tail == NULL) {
list->tail = node;
list->head = node;
} else {
list->tail->next = node;
list->tail = node;
}
}
/***********************************************************/
void deleteHead(List* list) {
// if list is empty or has one node
if (list->tail == list->head) {
// if not empty
if (list->head != NULL)
free(list->head);
initList(list);
} else {
Node* temp = list->head;
list->head = list->head->next;
free(temp);
}
}
/***********************************************************/
void deleteTail(List* list) {
// check if list is empty
if (list->tail == NULL)
return;
// check if there is only one node in the list
if (list->tail == list->head) {
// only one node in the list
free(list->head);
list->head = list->tail = NULL;
return;
}
// else there are more than one node in the list
// loop the list and keep a pointer on the node before the last
Node* prev;
Node* traversal = list->head;
while (traversal != NULL) {
prev = traversal;
traversal = traversal->next;
}
// free the last node
free(traversal);
// update the tail
list->tail = prev;
// update the last node's pointer
prev->next = NULL;
}
/***********************************************************/
// deletes a given node from a list
void deleteNode(List* list, Node* n) {
if (list->head == NULL)
return;
if (n == NULL)
return;
// check if node given exists in the list
if (!NodeExistsInList(list, n))
return;
// check if there is only one node in the list
if (list->tail == list->head) {
free(list->head);
list->head = list->tail = NULL;
return;
}
// loop the list and keep a pointer on the node before the node to be deleted
Node* prev;
Node* traversal = list->head;
while (traversal != n) {
prev = traversal;
traversal = traversal->next;
}
// update the "next" of the node before the node to be deleted
prev->next = traversal->next;
// update the tail if the node to be deleted was the last node
if (traversal == list->tail)
list->tail = prev;
//update the head if the node to be deleted was the first node in the list
if (traversal == list->head)
list->head = traversal->next;
// delete the node
free(traversal);
}
/***********************************************************/
int isEmpty(List* list) {
return list->head == NULL ? 1 : 0;
}
/***********************************************************/
Node* searchList(List* list, char* name) {
if (list == NULL || name == NULL)
return NULL;
// traversal pointer
Node* trav = list->head;
// loop the list looking for the node
while (trav != NULL) {
if (strcmp(trav->variable.name, name) == 0)
return trav;
trav = trav->next;
}
return NULL;
}
char* listToString(List* list) {
if (list == NULL)
return NULL;
// allocate memory for string
// assuming each variable will require 20 characters to represent it
int size = getListSize(list);
char* str = (char*) malloc(20 * size * sizeof(char));
// empty the string
str[0] = '\0';
if (isEmpty(list))
return NULL;
// loop through the list and add each term to the string
Node* n = list->head;
while (n != NULL) {
char* variable = VariableToString(n);
// concatenate the "variable" string to the polynomial string
strcat(str, variable);
// add a space to separate terms
strcat(str, " ");
// go to the next node
n = n->next;
}
return str;
}
/*********************************************************************************************/
/********************************* HASH TABLE FUNCTIONS **************************************/
/*********************************************************************************************/
void initHashTable(HashTable* t) {
int i;
for (i = 0; i < HTSIZE; i++) {
t->table[i] = NULL;
}
}
int Hash(char* variableName) {
if (variableName == NULL)
return -1;
if (!isalpha(variableName[0]))
return -1;
return tolower(variableName[0]) - 'a';
}
void addToHash(HashTable* t, char* name, double value) {
int hash = Hash(name);
if (t->table[hash] == NULL) {
t->table[hash] = (List*) malloc(sizeof(List));
}
List* l = t->table[hash];
Node* result = searchList(l, name);
if (result != NULL) {
// replace original variable's value with new value
result->variable.value = value;
} else {
addTail(l, name, value);
}
}
void addVariableToHash(HashTable* t, Variable v) {
addToHash(t, v.name, v.value);
}
double getValueForName(HashTable* t, char* name) {
if (t == NULL || name == NULL)
return INT_MIN;
int hash = Hash(name);
if (hash == -1) {
return INT_MIN;
}
List* l = t->table[hash];
Node* result = searchList(l, name);
if (result == NULL)
return INT_MIN;
return result->variable.value;
}
char* tableToString(HashTable* t) {
// get the total number of variable in the hashtable
int i, numOfVariable = 0;
for (i = 0; i < HTSIZE; i++) {
numOfVariable += getListSize(t->table[i]);
}
// allocate memory for string representation of hashtable
// assuming each variable will require 20 characters
char* str = (char*) malloc(20 * numOfVariable * sizeof(char));
// empty the string
str[0] = '\0';
// loop through the hashtable and add each list to the string
for (i = 0; i < HTSIZE; i++) {
char* list = listToString(t->table[i]);
// concatenate the "list" string to the string
if (list == NULL) {
strcat(str, "EMPTY LIST");
} else {
strcat(str, list);
}
// insert a newline to separate terms
strcat(str, "\n");
}
return str;
}
int main(void) {
HashTable t;
initHashTable(&t);
char a1[5] = "X";
char a2[5] = "xy";
char a3[5] = "xx";
char a4[5] = "a";
char a5[5] = "b";
char a6[5] = "A";
char a7[5] = "Z";
char a8[5] = "inc";
char a9[5] = "lsj";
char a10[5] = "sdf";
char a11[5] = "grx";
char a12[5] = "yfg";
char a13[5] = "dsf";
char a14[5] = "inc"; // notice a variable already exits with this name,
// the variable with this name in the list will have the
// newest value, i.e. old values are overwritten
addToHash(&t, a1, 5.0);
addToHash(&t, a2, 6.0);
addToHash(&t, a3, 7.0);
addToHash(&t, a4, 8.0);
addToHash(&t, a5, 9.0);
addToHash(&t, a6, 10.0);
addToHash(&t, a7, 11.0);
addToHash(&t, a8, 12.0);
addToHash(&t, a9, 13.0);
addToHash(&t, a10, 14.0);
addToHash(&t, a11, 15.0);
addToHash(&t, a12, 16.0);
addToHash(&t, a13, 17.0);
addToHash(&t, a14, 18.0);
printf("HashTable:\n%s", tableToString(&t));
return 0;
}