-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy path100-sorted_hash_table.c
220 lines (192 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
#include "hash_tables.h"
shash_table_t *shash_table_create(unsigned long int size);
int shash_table_set(shash_table_t *ht, const char *key, const char *value);
char *shash_table_get(const shash_table_t *ht, const char *key);
void shash_table_print(const shash_table_t *ht);
void shash_table_print_rev(const shash_table_t *ht);
void shash_table_delete(shash_table_t *ht);
/**
* shash_table_create - Creates a sorted hash table.
* @size: The size of new sorted hash table.
*
* Return: If an error occurs - NULL.
* Otherwise - a pointer to the new sorted hash table.
*/
shash_table_t *shash_table_create(unsigned long int size)
{
shash_table_t *ht;
unsigned long int i;
ht = malloc(sizeof(shash_table_t));
if (ht == NULL)
return (NULL);
ht->size = size;
ht->array = malloc(sizeof(shash_node_t *) * size);
if (ht->array == NULL)
return (NULL);
for (i = 0; i < size; i++)
ht->array[i] = NULL;
ht->shead = NULL;
ht->stail = NULL;
return (ht);
}
/**
* shash_table_set - Adds an element to a sorted hash table.
* @ht: A pointer to the sorted hash table.
* @key: The key to add - cannot be an empty string.
* @value: The value associated with key.
*
* Return: Upon failure - 0.
* Otherwise - 1.
*/
int shash_table_set(shash_table_t *ht, const char *key, const char *value)
{
shash_node_t *new, *tmp;
char *value_copy;
unsigned long int index;
if (ht == NULL || key == NULL || *key == '\0' || value == NULL)
return (0);
value_copy = strdup(value);
if (value_copy == NULL)
return (0);
index = key_index((const unsigned char *)key, ht->size);
tmp = ht->shead;
while (tmp)
{
if (strcmp(tmp->key, key) == 0)
{
free(tmp->value);
tmp->value = value_copy;
return (1);
}
tmp = tmp->snext;
}
new = malloc(sizeof(shash_node_t));
if (new == NULL)
{
free(value_copy);
return (0);
}
new->key = strdup(key);
if (new->key == NULL)
{
free(value_copy);
free(new);
return (0);
}
new->value = value_copy;
new->next = ht->array[index];
ht->array[index] = new;
if (ht->shead == NULL)
{
new->sprev = NULL;
new->snext = NULL;
ht->shead = new;
ht->stail = new;
}
else if (strcmp(ht->shead->key, key) > 0)
{
new->sprev = NULL;
new->snext = ht->shead;
ht->shead->sprev = new;
ht->shead = new;
}
else
{
tmp = ht->shead;
while (tmp->snext != NULL && strcmp(tmp->snext->key, key) < 0)
tmp = tmp->snext;
new->sprev = tmp;
new->snext = tmp->snext;
if (tmp->snext == NULL)
ht->stail = new;
else
tmp->snext->sprev = new;
tmp->snext = new;
}
return (1);
}
/**
* shash_table_get - Retrieve the value associated with
* a key in a sorted hash table.
* @ht: A pointer to the sorted hash table.
* @key: The key to get the value of.
*
* Return: If the key cannot be matched - NULL.
* Otherwise - the value associated with key in ht.
*/
char *shash_table_get(const shash_table_t *ht, const char *key)
{
shash_node_t *node;
unsigned long int index;
if (ht == NULL || key == NULL || *key == '\0')
return (NULL);
index = key_index((const unsigned char *)key, ht->size);
if (index >= ht->size)
return (NULL);
node = ht->shead;
while (node != NULL && strcmp(node->key, key) != 0)
node = node->snext;
return ((node == NULL) ? NULL : node->value);
}
/**
* shash_table_print - Prints a sorted hash table in order.
* @ht: A pointer to the sorted hash table.
*/
void shash_table_print(const shash_table_t *ht)
{
shash_node_t *node;
if (ht == NULL)
return;
node = ht->shead;
printf("{");
while (node != NULL)
{
printf("'%s': '%s'", node->key, node->value);
node = node->snext;
if (node != NULL)
printf(", ");
}
printf("}\n");
}
/**
* shash_table_print_rev - Prints a sorted hash table in reverse order.
* @ht: A pointer to the sorted hash table to print.
*/
void shash_table_print_rev(const shash_table_t *ht)
{
shash_node_t *node;
if (ht == NULL)
return;
node = ht->stail;
printf("{");
while (node != NULL)
{
printf("'%s': '%s'", node->key, node->value);
node = node->sprev;
if (node != NULL)
printf(", ");
}
printf("}\n");
}
/**
* shash_table_delete - Deletes a sorted hash table.
* @ht: A pointer to the sorted hash table.
*/
void shash_table_delete(shash_table_t *ht)
{
shash_table_t *head = ht;
shash_node_t *node, *tmp;
if (ht == NULL)
return;
node = ht->shead;
while (node)
{
tmp = node->snext;
free(node->key);
free(node->value);
free(node);
node = tmp;
}
free(head->array);
free(head);
}