-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhashtable.c
More file actions
114 lines (96 loc) · 2.59 KB
/
hashtable.c
File metadata and controls
114 lines (96 loc) · 2.59 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
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
/*Hash table with string as key and int as value*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 128
struct hashElement
{
char *key;
int value;
};
struct hashTable
{
struct hashElement **table;
};
typedef struct hashElement HashElement;
typedef struct hashTable HashTable;
int hash(char *);
HashTable *newHashTable();
HashElement *newHashElement(char *key, int value);
void insert(HashTable *table, char *key, int value);
void delete(HashTable *table, char *key);
int search(HashTable *table, char *key);
int main()
{
HashTable *h = newHashTable();
insert(h, "3", 6);
printf("%d\n", search(h, "3"));
delete(h, "3");
printf("%d\n", search(h, "3"));
insert(h, "2048", 10);
printf("%d\n", search(h, "2048"));
}
int hash(char *str)
{
int sum=0;
for(int i=0, n=strlen(str); i<n; i++)
{
sum += (int)str[i];
}
return sum%SIZE;
}
HashTable *newHashTable()
{
HashTable *t = (HashTable *)malloc(sizeof(HashTable));
//t->table = HashElement **
t->table = (HashElement **)malloc(SIZE*sizeof(HashElement *));
for(int i=0; i<SIZE; i++) t->table[i]=NULL;
return t;
}
HashElement *newHashElement(char *key, int value)
{
HashElement *newEntry = (HashElement *)malloc(sizeof(HashElement));
newEntry->key= (char *)malloc(20*sizeof(char));
strcpy(newEntry->key, key);
newEntry->value = value;
return newEntry;
}
void insert(HashTable *t, char *key, int value)
{
int hashedkey = hash(key);
while(t->table[hashedkey]!=NULL && (strcmp(t->table[hashedkey]->key, key) != 0))
{
hashedkey = (hashedkey+1)%SIZE;
}
t->table[hashedkey] = (HashElement *)malloc(sizeof(HashElement));
t->table[hashedkey] = newHashElement(key, value);
}
void delete(HashTable *t, char *key)
{
int hashedkey = hash(key);
while(t->table[hashedkey]!=NULL)
{
if(strcmp(t->table[hashedkey]->key, key) == 0) break;
hashedkey = (hashedkey+1)%SIZE;
}
if(t->table[hashedkey]==NULL)
{
printf("Nothing found for key %s\n", key);
return;
}
else t->table[hashedkey]=NULL;
}
int search(HashTable *t, char *key)
{
int hashedkey = hash(key);
while(t->table[hashedkey]!=NULL && (strcmp(t->table[hashedkey]->key, key) != 0))
{
hashedkey = (hashedkey+1)%SIZE;
}
if(t->table[hashedkey]==NULL)
{
printf("Nothing found for key %s\n", key);
return -1;
}
return t->table[hashedkey]->value;
}