-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-sorted_hash_table.c
249 lines (233 loc) · 4.88 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
#include "hash_tables.h"
/**
* shash_table_create - creates a sorted hash table
* @size: size of the hash table
*
* Return: pointer to the new table, or NULL on failure
*/
shash_table_t *shash_table_create(unsigned long int size)
{
shash_table_t *sht;
unsigned long int i;
sht = malloc(sizeof(shash_table_t));
if (sht == NULL)
return (NULL);
sht->size = size;
sht->shead = NULL;
sht->stail = NULL;
sht->array = malloc(sizeof(shash_node_t) * size);
if (sht->array == NULL)
{
free(sht);
return (NULL);
}
for (i = 0; i < size; i++)
{
sht->array[i] = NULL;
}
return (sht);
}
/**
* make_shash_node - makes a node for the sorted hash table
* @key: key for the data
* @value: data to be stored
*
* 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 *shn;
shn = malloc(sizeof(shash_node_t));
if (shn == NULL)
return (NULL);
shn->key = strdup(key);
if (shn->key == NULL)
{
free(shn);
return (NULL);
}
shn->value = strdup(value);
if (shn->value == NULL)
{
free(shn->key);
free(shn);
return (NULL);
}
shn->next = shn->snext = shn->sprev = NULL;
return (shn);
}
/**
* add_to_sorted_list - add a node to the sorted (by key's ASCII) linked list
* @table: the sorted hash table
* @node: the node to add
*
* Return: void
*/
void add_to_sorted_list(shash_table_t *table, shash_node_t *node)
{
shash_node_t *tmp;
if (table->shead == NULL && table->stail == NULL)
{
table->shead = table->stail = node;
return;
}
tmp = table->shead;
while (tmp != NULL)
{
if (strcmp(node->key, tmp->key) < 0)
{
node->snext = tmp;
node->sprev = tmp->sprev;
tmp->sprev = node;
if (node->sprev != NULL)
node->sprev->snext = node;
else
table->shead = node;
return;
}
tmp = tmp->snext;
}
node->sprev = table->stail;
table->stail->snext = node;
table->stail = node;
}
/**
* shash_table_set - sets a key to a value in the hash table
* @ht: sorted hash table
* @key: key to the data
* @value: data to add
*
* Return: 1 on success, 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 *shn, *tmp;
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);
tmp = ht->array[index];
while (tmp != NULL)
{
if (strcmp(tmp->key, key) == 0)
{
new_value = strdup(value);
if (new_value == NULL)
return (0);
free(tmp->value);
tmp->value = new_value;
return (1);
}
tmp = tmp->next;
}
shn = make_shash_node(key, value);
if (shn == NULL)
return (0);
shn->next = ht->array[index];
ht->array[index] = shn;
add_to_sorted_list(ht, shn);
return (1);
}
/**
* shash_table_get - retrieve a value from the hash table
* @ht: hash table
* @key: key to the data
*
* Return: the value associated with key, or NULL on failure
*/
char *shash_table_get(const shash_table_t *ht, const char *key)
{
unsigned long int index;
shash_node_t *tmp;
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);
tmp = ht->array[index];
while (tmp != NULL)
{
if (strcmp(tmp->key, key) == 0)
return (tmp->value);
tmp = tmp->next;
}
return (NULL);
}
/**
* shash_table_print - prints a sorted hash table
* @ht: hash table to print
*
* Return: void
*/
void shash_table_print(const shash_table_t *ht)
{
shash_node_t *tmp;
char flag = 0; /* 0 before printing any data, 1 after*/
if (ht == NULL || ht->array == NULL)
return;
printf("{");
tmp = ht->shead;
while (tmp != NULL)
{
if (flag == 1)
printf(", ");
printf("'%s': '%s'", tmp->key, tmp->value);
flag = 1;
tmp = tmp->snext;
}
printf("}\n");
}
/**
* shash_table_print_rev - prints a sorted hash table in reverse
* @ht: hash table to print
*
* Return: void
*/
void shash_table_print_rev(const shash_table_t *ht)
{
shash_node_t *tmp;
char flag = 0; /* 0 before printing any data, 1 after*/
if (ht == NULL || ht->array == NULL)
return;
printf("{");
tmp = ht->stail;
while (tmp != NULL)
{
if (flag == 1)
printf(", ");
printf("'%s': '%s'", tmp->key, tmp->value);
flag = 1;
tmp = tmp->sprev;
}
printf("}\n");
}
/**
* shash_table_delete - deletes a sorted hash table
* @ht: hash table to delete
*
* Return: void
*/
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);
}