-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-sorted_hash_table.c
273 lines (220 loc) · 4.42 KB
/
100-sorted_hash_table.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "hash_tables.h"
/**
* shash_table_create - creates a shash table with a given size
*
* @size: size of the shash table
* Return: the created shash table, or NULL if function fails
*/
shash_table_t *shash_table_create(unsigned long int size)
{
shash_table_t *table;
shash_node_t **array;
unsigned long int i;
table = malloc(sizeof(shash_table_t));
if (table == NULL)
return (NULL);
array = malloc(sizeof(shash_node_t *) * size);
if (array == NULL)
return (NULL);
for (i = 0; i < size; i++)
array[i] = NULL;
table->array = array;
table->size = size;
table->shead = NULL;
table->stail = NULL;
return (table);
}
/**
* add_n_shash - adds a node at the beginning of a shash at a given index
*
* @h: head of the shash linked list
* @key: key of the shash
* @value: value to store
* Return: created node
*/
shash_node_t *add_n_shash(shash_node_t **h, const char *key, const char *value)
{
shash_node_t *tmp;
tmp = *h;
while (tmp != NULL)
{
if (strcmp(key, tmp->key) == 0)
{
free(tmp->value);
tmp->value = strdup(value);
return (tmp);
}
tmp = tmp->next;
}
tmp = malloc(sizeof(shash_node_t));
if (tmp == NULL)
return (NULL);
tmp->key = strdup(key);
tmp->value = strdup(value);
tmp->next = *h;
*h = tmp;
return (tmp);
}
/**
* add_i_shash - adds a node on the DLL of the shash table
*
* @ht: pointer to the table
* @new: new node to add
* Return: no return
*/
void add_i_shash(shash_table_t *ht, shash_node_t *new)
{
shash_node_t *tmp1, *tmp2;
int ret;
tmp1 = tmp2 = ht->shead;
while (tmp1 != NULL)
{
ret = strcmp(new->key, tmp1->key);
if (ret == 0)
{
return;
}
else if (ret < 0)
{
new->sprev = tmp1->sprev;
if (tmp1->sprev)
tmp1->sprev->snext = new;
else
ht->shead = new;
tmp1->sprev = new;
new->snext = tmp1;
return;
}
tmp2 = tmp1;
tmp1 = tmp1->snext;
}
new->sprev = tmp2;
new->snext = NULL;
if (ht->shead)
tmp2->snext = new;
else
ht->shead = new;
ht->stail = new;
}
/**
* shash_table_set - adds a hash (key, value) to a given shash table
*
* @ht: pointer to the shash table
* @key: key of the shash
* @value: value to store
* Return: 1 if successes, 0 if fails
*/
int shash_table_set(shash_table_t *ht, const char *key, const char *value)
{
unsigned long int k_index;
shash_node_t *new;
if (ht == NULL)
return (0);
if (key == NULL || *key == '\0')
return (0);
k_index = key_index((unsigned char *)key, ht->size);
new = add_n_shash(&(ht->array[k_index]), key, value);
if (new == NULL)
return (0);
add_i_shash(ht, new);
return (1);
}
/**
* shash_table_get - retrieves a value associated with a key
*
* @ht: pointer to the shash table
* @key: key of the shash
* Return: value of the shash.
*/
char *shash_table_get(const shash_table_t *ht, const char *key)
{
unsigned long int k_index;
shash_node_t *tmp;
if (ht == NULL)
return (NULL);
if (key == NULL || *key == '\0')
return (NULL);
k_index = key_index((unsigned char *)key, ht->size);
tmp = ht->array[k_index];
while (tmp != NULL)
{
if (strcmp(tmp->key, key) == 0)
return (tmp->value);
tmp = tmp->next;
}
return (NULL);
}
/**
* shash_table_print - prints the keys and values of the shash table
*
* @ht: pointer to the shash table
* Return: no return
*/
void shash_table_print(const shash_table_t *ht)
{
shash_node_t *tmp;
char *sep;
if (ht == NULL)
return;
printf("{");
sep = "";
tmp = ht->shead;
while (tmp != NULL)
{
printf("%s'%s': '%s'", sep, tmp->key, tmp->value);
sep = ", ";
tmp = tmp->snext;
}
printf("}\n");
}
/**
* shash_table_print_rev - prints the keys and values of the shash table
* in reverse
*
* @ht: pointer to the shash table
* Return: no return
*/
void shash_table_print_rev(const shash_table_t *ht)
{
shash_node_t *tmp;
char *sep;
if (ht == NULL)
return;
printf("{");
sep = "";
tmp = ht->stail;
while (tmp != NULL)
{
printf("%s'%s': '%s'", sep, tmp->key, tmp->value);
sep = ", ";
tmp = tmp->sprev;
}
printf("}\n");
}
/**
* shash_table_delete - deletes a shash table
*
* @ht: pointer to the shash table
* Return: no return
*/
void shash_table_delete(shash_table_t *ht)
{
unsigned long int i;
shash_node_t *tmp1;
shash_node_t *tmp2;
if (ht == NULL)
return;
for (i = 0; i < ht->size; i++)
{
tmp1 = ht->array[i];
while ((tmp2 = tmp1) != NULL)
{
tmp1 = tmp1->next;
free(tmp2->key);
free(tmp2->value);
free(tmp2);
}
}
free(ht->array);
free(ht);
}