-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashmap.c
63 lines (56 loc) · 1.67 KB
/
hashmap.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
/********************************************
2017B4A70696P Abdul Kadir Khimani
********************************************/
#include "hashmap.h"
int hashFunction(char *arr){
int hashValue = 0;
int len = strlen(arr);
int i;
for(i = 0; i < len; i++){
hashValue += arr[i];
hashValue %= HASH_DIM;
}
return hashValue;
}
/*This function creates and fills the hashtable*/
tableEntry** initialiseTable(){
int i;
tableEntry** T = (tableEntry**)malloc(sizeof(tableEntry*) * HASH_DIM);
for(i = 0; i < HASH_DIM; i++) //initializing all slots in hash table to NULL pointers
T[i] = NULL;
return T;
}
void addtoTable(tableEntry* t, tableEntry **T){
int key = hashFunction(t->name);
if(T[key] != NULL){
tableEntry *temp = T[key];
while(temp->next!=NULL)
temp = temp->next;
temp->next = t;
}
else
T[key] = t;
// printf("Table Entry Added\n");
}
/*This function looks up (char*)identifier in the (tableEntry**)hash_table.
Returns : NULL; if not keyword,
pointer to associated keyword's token; if keyword*/
tableEntry* lookup(char *identifier, tableEntry **T){
int id_hash = hashFunction(identifier);
tableEntry *temp = T[id_hash];
while(temp != NULL){
if(strcmp(identifier, temp->name) == 0)
return temp;
else
temp = temp->next;
}
return NULL;
}
tableEntry* createTableEntry(int type, char *name, char *arrayType){
tableEntry* t = (tableEntry*)malloc(sizeof(tableEntry));
t->type = type;
strcpy(t->name, name);
strcpy(t->arrayType, arrayType);
t->next = NULL;
return t;
}