-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhashTable.c
More file actions
87 lines (80 loc) · 2.03 KB
/
hashTable.c
File metadata and controls
87 lines (80 loc) · 2.03 KB
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
// Batch Number - 38
// 2014A7PS102P - RISHABH JOSHI
// 2014A7PS248P - DEEP VYAS
#include "hashTable.h"
#include <stdlib.h>
#include <string.h>
HashCell *HashTable;
void initTable(){
HashCell *table;
table = (HashCell*)malloc(TABLE_SIZE*sizeof(HashCell));
int i =0;
for(i = 0; i < TABLE_SIZE; i++){
table[i].head = NULL;
}
populate(table);
HashTable = table;
}
int hashKey(char *key){
int res=0;
int l=strlen(key);
int i;
for(i=l-1;i>=0;i--){
res+=key[i]-'0';
}
return res%TABLE_SIZE;
}
HashNode* find(char *key,HashCell* table){
int hash = hashKey(key);
if(table[hash].head==NULL) return NULL;
else{
HashNode *itr=table[hash].head;
while(itr!=NULL){
if(strcmp(key,itr->key)==0) return itr;
itr=itr->next;
}
return NULL;
}
}
void addKey(char *key,TokenType token,HashCell **table){
//printf("%s\n",key);
int hash = hashKey(key);
HashNode *tmp = (HashNode*)malloc(sizeof(HashNode));
strcpy(tmp->key,key);
tmp->token=token;
tmp->next=(*table)[hash].head;
(*table)[hash].head=tmp;
// printf("%s\n", (*table)[hash].head->key);
}
void populate(HashCell *table){
addKey("integer",INTEGER, &table);
addKey("real",REAL,&table);
addKey("boolean",BOOLEAN, &table);
addKey("of",OF,&table);
addKey("array",ARRAY,&table);
addKey("start",START,&table);
addKey("end",END,&table);
addKey("declare",DECLARE,&table);
addKey("module",MODULE,&table);
addKey("driver",DRIVER,&table);
addKey("program",PROGRAM,&table);
addKey("get_value",GET_VALUE,&table);
addKey("print",PRINT,&table);
addKey("use",USE,&table);
addKey("with",WITH,&table);
addKey("parameters",PARAMETERS,&table);
addKey("true",TRUE,&table);
addKey("false",FALSE,&table);
addKey("takes",TAKES,&table);
addKey("input",INPUT,&table);
addKey("returns",RETURNS,&table);
addKey("AND",AND,&table);
addKey("OR",OR,&table);
addKey("for",FOR,&table);
addKey("in",IN,&table);
addKey("switch",SWITCH,&table);
addKey("case",CASE,&table);
addKey("break",BREAK,&table);
addKey("default",DEFAULT,&table);
addKey("while",WHILE,&table);
}