-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-sorted_hash_table.c
257 lines (242 loc) · 5.82 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash_tables.h"
/**
* shash_table_create - function that creates a hash table.
*
* If something went wrong, your function should return NULL.
*
* @size: size of the hash table/array.
* Return: pointer to the newly created hash table.
*/
shash_table_t *shash_table_create(unsigned long int size)
{
shash_table_t *sh_table;
unsigned long int i;
sh_table = malloc(sizeof(shash_table_t));
if (sh_table == NULL)
return (NULL);
sh_table->size = size;
sh_table->shead = NULL;
sh_table->stail = NULL;
sh_table->array = malloc(sizeof(shash_node_t) * size);
if (sh_table->array == NULL)
{
free(sh_table);
return (NULL);
}
for (i = 0; i < size; i++)
{
sh_table->array[i] = NULL;
}
return (sh_table);
}
/**
* make_shash_node - function that makes a node for the sorted hash table.
* @key: pointer to key.
* @value: pointer to the value value associated with the key.
*
* Return: pointer to the new node, or NULL on failure.
*/
shash_node_t *make_shash_node(const char *key, const char *value)
{
shash_node_t *sh_node;
sh_node = malloc(sizeof(shash_node_t));
if (sh_node == NULL)
return (NULL);
sh_node->key = strdup(key);
if (sh_node->key == NULL)
{
free(sh_node);
return (NULL);
}
sh_node->value = strdup(value);
if (sh_node->value == NULL)
{
free(sh_node->key);
free(sh_node);
return (NULL);
}
sh_node->next = sh_node->snext = sh_node->sprev = NULL;
return (sh_node);
}
/**
* add_to_sorted_list - adds a node to the sorted (by key's ASCII),
* linked list.
* @table: pointer to sorted hash table.
* @node: pointer to node to add.
*
* Return: No return.
*/
void add_to_sorted_list(shash_table_t *table, shash_node_t *node)
{
shash_node_t *temp_var;
if (table->shead == NULL && table->stail == NULL)
{
table->shead = table->stail = node;
return;
}
temp_var = table->shead;
while (temp_var != NULL)
{
if (strcmp(node->key, temp_var->key) < 0)
{
node->snext = temp_var;
node->sprev = temp_var->sprev;
temp_var->sprev = node;
if (node->sprev != NULL)
node->sprev->snext = node;
else
table->shead = node;
return;
}
temp_var = temp_var->snext;
}
node->sprev = table->stail;
table->stail->snext = node;
table->stail = node;
}
/**
* shash_table_set - function that adds an element to the hash table.
* @ht: pointer to hash table you want to add or update the key/value to.
* @key: pointer to key and cannot be an empty string.
* @value: pointer to the value value associated with the key.
*
* value must be duplicated. value can be an empty string.
* In case of collision, add the new_node node at the beginning of the list.
*
* Return: 1 if it succeeded, 0 otherwise.
*/
int shash_table_set(shash_table_t *ht, const char *key, const char *value)
{
unsigned long int index;
char *new_value;
shash_node_t *sh_node, *temp_var;
if (ht == NULL || ht->array == NULL || ht->size == 0 ||
key == NULL || strlen(key) == 0 || value == NULL)
return (0);
index = key_index((const unsigned char *)key, ht->size);
temp_var = ht->array[index];
while (temp_var != NULL)
{
if (strcmp(temp_var->key, key) == 0)
{
new_value = strdup(value);
if (new_value == NULL)
return (0);
free(temp_var->value);
temp_var->value = new_value;
return (1);
}
temp_var = temp_var->next;
}
sh_node = make_shash_node(key, value);
if (sh_node == NULL)
return (0);
sh_node->next = ht->array[index];
ht->array[index] = sh_node;
add_to_sorted_list(ht, sh_node);
return (1);
}
/**
* shash_table_get - function that retrieves a value associated with a key.
* @ht: pointer to hash table to look into.
* @key: pointer to key to looking for.
*
* Return: value associated with the element, or NULL.
*/
char *shash_table_get(const shash_table_t *ht, const char *key)
{
unsigned long int index;
shash_node_t *temp_var;
if (ht == NULL || ht->array == NULL || ht->size == 0 ||
key == NULL || strlen(key) == 0)
return (NULL);
index = key_index((const unsigned char *)key, ht->size);
temp_var = ht->array[index];
while (temp_var != NULL)
{
if (strcmp(temp_var->key, key) == 0)
return (temp_var->value);
temp_var = temp_var->next;
}
return (NULL);
}
/**
* shash_table_print - function that prints a hash table.
* @ht: pointer to hash table to be printed.
*
* Return: No return.
*/
void shash_table_print(const shash_table_t *ht)
{
shash_node_t *temp_var;
char flag = 0; /* 0 before printing any data, 1 after*/
if (ht == NULL || ht->array == NULL)
return;
printf("{");
temp_var = ht->shead;
while (temp_var != NULL)
{
if (flag == 1)
printf(", ");
printf("'%s': '%s'", temp_var->key, temp_var->value);
flag = 1;
temp_var = temp_var->snext;
}
printf("}\n");
}
/**
* shash_table_print_rev - function that prints a sorted hash table in reverse.
* @ht: pointer to hash table to be printed.
*
* Return: No return.
*/
void shash_table_print_rev(const shash_table_t *ht)
{
shash_node_t *temp_var;
char flag = 0; /* 0 before printing any data, 1 after*/
if (ht == NULL || ht->array == NULL)
return;
printf("{");
temp_var = ht->stail;
while (temp_var != NULL)
{
if (flag == 1)
printf(", ");
printf("'%s': '%s'", temp_var->key, temp_var->value);
flag = 1;
temp_var = temp_var->sprev;
}
printf("}\n");
}
/**
* shash_table_delete - function that deletes a hash table.
* @ht: pointer to hash table to be deleted.
*
* Return: No return.
*/
void shash_table_delete(shash_table_t *ht)
{
unsigned long int i;
shash_node_t *next;
if (ht == NULL || ht->array == NULL || ht->size == 0)
return;
for (i = 0; i < ht->size; i++)
{
while (ht->array[i] != NULL)
{
next = ht->array[i]->next;
free(ht->array[i]->key);
free(ht->array[i]->value);
free(ht->array[i]);
ht->array[i] = next;
}
}
free(ht->array);
ht->array = NULL;
ht->shead = ht->stail = NULL;
ht->size = 0;
free(ht);
}