-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhashtable.c
More file actions
170 lines (144 loc) · 3.13 KB
/
hashtable.c
File metadata and controls
170 lines (144 loc) · 3.13 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
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
/* Hash table
* January 13, 2022 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_t {
char *key;
char *value;
struct list_t *next;
} list;
typedef struct hashTable_t {
unsigned int size;
list **array;
/* each cell of this array is a pointer to the first node of a
* linked list */
} hashTable;
static hashTable *ht_create(unsigned int size) {
hashTable *ht;
if (size < 1)
return NULL;
if (!(ht = (hashTable *)malloc(sizeof(hashTable)))) {
fprintf(stderr, "Could not create hash table\n");
return NULL;
}
ht->array = (list **)malloc(size * sizeof(list));
if (!ht->array) {
fprintf(stderr, "Could not create first list in hash table\n");
return NULL;
}
memset(ht->array, 0, size * sizeof(list));
ht->size = size;
return ht;
}
/* free the items in a hashTable. free each individual row and column */
static void ht_free(hashTable *ht) {
list *tmp;
if (!ht)
return;
for (unsigned int i = 0; i < ht->size; ++i) {
if (ht->array[i] != NULL) {
/* traverse the list and free nodes */
while (ht->array[i] != NULL) {
tmp = ht->array[i]->next;
free(ht->array[i]->key);
free(ht->array[i]->value);
free(ht->array[i]);
ht->array[i] = tmp;
}
free(ht->array[i]);
}
}
free(ht->array);
free(ht);
}
static unsigned int hash(const char *key, unsigned int size) {
unsigned int hash = 0, i = 0;
while (key && key[i]) {
hash = (hash + key[i]) % size;
++i;
}
return hash;
}
static void node_handle(hashTable *ht, list *node) {
unsigned int i = hash(node->key, ht->size);
list *tmp = ht->array[i];
if (ht->array[i] != NULL) {
tmp = ht->array[i];
while (tmp != NULL) {
if (strcmp(tmp->key, node->key) == 0)
break;
tmp = tmp->next;
}
if (tmp == NULL) {
node->next = ht->array[i];
ht->array[i] = node;
}
else {
free(tmp->value);
tmp->value = strdup(node->value);
free(node->value);
free(node->key);
free(node);
}
}
else {
node->next = NULL;
ht->array[i] = node;
}
}
static int ht_put(hashTable *ht, const char *key, const char *value) {
list *node;
if (!ht) {
fprintf(stderr, "Hashtable does not exist\n");
return 1;
}
if (!(node = malloc(sizeof(list)))) {
fprintf(stderr, "Could not allocate node\n");
return 1;
}
node->key = strdup(key);
node->value = strdup(value);
node_handle(ht, node);
return 0;
}
static char *ht_get(hashTable *ht, const char *key) {
char *key_cp;
unsigned int i;
list *tmp;
if (!ht) {
return NULL;
}
key_cp = strdup(key);
i = hash(key, ht->size);
tmp = ht->array[i];
while (tmp != NULL) {
if (strcmp(tmp->key, key_cp) == 0) {
break;
}
tmp = tmp->next;
}
free(key_cp);
if (tmp == NULL) {
return NULL;
}
return tmp->value;
}
int main(void) {
hashTable *ht;
if (!(ht = ht_create(1))) {
fprintf(stderr, "Could not create hash table\n");
return 1;
}
if (ht_put(ht, "hello", "world") == 0) { /* push two rows */
if (ht_put(ht, "foo", "bar") == 0) {
printf("\"foo\" = %s\n", ht_get(ht, "foo"));
/* get second row by key "foo" */
}
printf("\"hello\" = %s\n", ht_get(ht, "hello"));
/* get first row by key "hello" */
ht_free(ht);
return 0;
}
return 1;
}