-
Notifications
You must be signed in to change notification settings - Fork 2
/
hashtable.c
100 lines (89 loc) · 1.87 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
/*
BATCH NO. 27
Mayank Agarwal (2014A7PS111P)
Karan Deep Batra(2014A7PS160P)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hashtable.h"
#include "ntort.h"
int hash(char* str)//hash from string to bucket number
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return (int)(hash % hashtablesize);
}
void printhashtable(hashtable* table) //print hash table
{
for(int i=0; i<hashtablesize; i++)
{
printf("%d -> ",i);
ntort* pt = table->buckets[i];
while(pt != NULL)
{
printntortnode(pt);
pt=pt->next;
}
printf("\n");
}
return;
}
hashtable* makehashtable() //returns pointer to new hash table
{
hashtable* temp = (hashtable*)malloc(sizeof(hashtable));
for(int i=0; i<hashtablesize; i++)
temp->buckets[i] = NULL;
return temp;
}
int present(hashtable* table, char* str) //checks if str is present in table and returns corresponding mapping to integer
{
int h = hash(str);
ntort* pt = table->buckets[h];
while(pt != NULL)
{
if(strcmp(str,pt->str) == 0)
return pt->val;
pt = pt->next;
}
return 0;
}
void insert(hashtable* table, char* str) //inserts str to table and assigns number according to terminal or non terminal
{
// printf("%s\n",str);
static int ntval=1, tval=1;
int h = hash(str);
ntort* pt = table->buckets[h];
int nt;
if(str[0] == '<')
{
nt = 1;
ntort* newnode = makentortnode(nt, ntval,str);
ntval++;
newnode->next = pt;
table->buckets[h] = newnode;
}
else
{
nt = 0;
ntort* newnode = makentortnode(nt, tval,str);
tval++;
newnode->next = pt;
table->buckets[h] = newnode;
}
return;
}
ntort* getnodehashtable(hashtable* table, char* str) //finds and returns node from hashtable table
{
int h = hash(str);
ntort* pt = table->buckets[h];
while(pt != NULL)
{
if(strcmp(str,pt->str) == 0)
return pt;
pt = pt->next;
}
return pt;
}