Skip to content

Commit 418406d

Browse files
committed
Update files
1 parent eb5efef commit 418406d

File tree

4 files changed

+321
-0
lines changed

4 files changed

+321
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
2+
#include "hash_tables.h"
3+
4+
shash_table_t *shash_table_create(unsigned long int size);
5+
int shash_table_set(shash_table_t *ht, const char *key, const char *value);
6+
char *shash_table_get(const shash_table_t *ht, const char *key);
7+
void shash_table_print(const shash_table_t *ht);
8+
void shash_table_print_rev(const shash_table_t *ht);
9+
void shash_table_delete(shash_table_t *ht);
10+
11+
/**
12+
* shash_table_create - Creates a sorted hash table.
13+
* @size: The size of new sorted hash table.
14+
*
15+
* Return: If an error occurs - NULL.
16+
* Otherwise - a pointer to the new sorted hash table.
17+
*/
18+
shash_table_t *shash_table_create(unsigned long int size)
19+
{
20+
shash_table_t *ht;
21+
unsigned long int i;
22+
23+
ht = malloc(sizeof(shash_table_t));
24+
if (ht == NULL)
25+
return (NULL);
26+
27+
ht->size = size;
28+
ht->array = malloc(sizeof(shash_node_t *) * size);
29+
if (ht->array == NULL)
30+
return (NULL);
31+
for (i = 0; i < size; i++)
32+
ht->array[i] = NULL;
33+
ht->shead = NULL;
34+
ht->stail = NULL;
35+
36+
return (ht);
37+
}
38+
39+
/**
40+
* shash_table_set - Adds an element to a sorted hash table.
41+
* @ht: A pointer to the sorted hash table.
42+
* @key: The key to add - cannot be an empty string.
43+
* @value: The value associated with key.
44+
*
45+
* Return: Upon failure - 0.
46+
* Otherwise - 1.
47+
*/
48+
int shash_table_set(shash_table_t *ht, const char *key, const char *value)
49+
{
50+
shash_node_t *new, *tmp;
51+
char *value_copy;
52+
unsigned long int index;
53+
54+
if (ht == NULL || key == NULL || *key == '\0' || value == NULL)
55+
return (0);
56+
57+
value_copy = strdup(value);
58+
if (value_copy == NULL)
59+
return (0);
60+
61+
index = key_index((const unsigned char *)key, ht->size);
62+
tmp = ht->shead;
63+
while (tmp)
64+
{
65+
if (strcmp(tmp->key, key) == 0)
66+
{
67+
free(tmp->value);
68+
tmp->value = value_copy;
69+
return (1);
70+
}
71+
tmp = tmp->snext;
72+
}
73+
74+
new = malloc(sizeof(shash_node_t));
75+
if (new == NULL)
76+
{
77+
free(value_copy);
78+
return (0);
79+
}
80+
new->key = strdup(key);
81+
if (new->key == NULL)
82+
{
83+
free(value_copy);
84+
free(new);
85+
return (0);
86+
}
87+
new->value = value_copy;
88+
new->next = ht->array[index];
89+
ht->array[index] = new;
90+
91+
if (ht->shead == NULL)
92+
{
93+
new->sprev = NULL;
94+
new->snext = NULL;
95+
ht->shead = new;
96+
ht->stail = new;
97+
}
98+
else if (strcmp(ht->shead->key, key) > 0)
99+
{
100+
new->sprev = NULL;
101+
new->snext = ht->shead;
102+
ht->shead->sprev = new;
103+
ht->shead = new;
104+
}
105+
else
106+
{
107+
tmp = ht->shead;
108+
while (tmp->snext != NULL && strcmp(tmp->snext->key, key) < 0)
109+
tmp = tmp->snext;
110+
new->sprev = tmp;
111+
new->snext = tmp->snext;
112+
if (tmp->snext == NULL)
113+
ht->stail = new;
114+
else
115+
tmp->snext->sprev = new;
116+
tmp->snext = new;
117+
}
118+
119+
return (1);
120+
}
121+
122+
/**
123+
* shash_table_get - Retrieve the value associated with
124+
* a key in a sorted hash table.
125+
* @ht: A pointer to the sorted hash table.
126+
* @key: The key to get the value of.
127+
*
128+
* Return: If the key cannot be matched - NULL.
129+
* Otherwise - the value associated with key in ht.
130+
*/
131+
char *shash_table_get(const shash_table_t *ht, const char *key)
132+
{
133+
shash_node_t *node;
134+
unsigned long int index;
135+
136+
if (ht == NULL || key == NULL || *key == '\0')
137+
return (NULL);
138+
139+
index = key_index((const unsigned char *)key, ht->size);
140+
if (index >= ht->size)
141+
return (NULL);
142+
143+
node = ht->shead;
144+
while (node != NULL && strcmp(node->key, key) != 0)
145+
node = node->snext;
146+
147+
return ((node == NULL) ? NULL : node->value);
148+
}
149+
150+
/**
151+
* shash_table_print - Prints a sorted hash table in order.
152+
* @ht: A pointer to the sorted hash table.
153+
*/
154+
void shash_table_print(const shash_table_t *ht)
155+
{
156+
shash_node_t *node;
157+
158+
if (ht == NULL)
159+
return;
160+
161+
node = ht->shead;
162+
printf("{");
163+
while (node != NULL)
164+
{
165+
printf("'%s': '%s'", node->key, node->value);
166+
node = node->snext;
167+
if (node != NULL)
168+
printf(", ");
169+
}
170+
printf("}\n");
171+
}
172+
173+
/**
174+
* shash_table_print_rev - Prints a sorted hash table in reverse order.
175+
* @ht: A pointer to the sorted hash table to print.
176+
*/
177+
void shash_table_print_rev(const shash_table_t *ht)
178+
{
179+
shash_node_t *node;
180+
181+
if (ht == NULL)
182+
return;
183+
184+
node = ht->stail;
185+
printf("{");
186+
while (node != NULL)
187+
{
188+
printf("'%s': '%s'", node->key, node->value);
189+
node = node->sprev;
190+
if (node != NULL)
191+
printf(", ");
192+
}
193+
printf("}\n");
194+
}
195+
196+
/**
197+
* shash_table_delete - Deletes a sorted hash table.
198+
* @ht: A pointer to the sorted hash table.
199+
*/
200+
void shash_table_delete(shash_table_t *ht)
201+
{
202+
shash_table_t *head = ht;
203+
shash_node_t *node, *tmp;
204+
205+
if (ht == NULL)
206+
return;
207+
208+
node = ht->shead;
209+
while (node)
210+
{
211+
tmp = node->snext;
212+
free(node->key);
213+
free(node->value);
214+
free(node);
215+
node = tmp;
216+
}
217+
218+
free(head->array);
219+
free(head);
220+
}

0x1A-hash_tables/4-hash_table_get.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#include "hash_tables.h"
3+
4+
/**
5+
* hash_table_get - Retrieve the value associated with
6+
* a key in a hash table.
7+
* @ht: A pointer to the hash table.
8+
* @key: The key to get the value of.
9+
*
10+
* Return: If the key cannot be matched - NULL.
11+
* Otherwise - the value associated with key in ht.
12+
*/
13+
char *hash_table_get(const hash_table_t *ht, const char *key)
14+
{
15+
hash_node_t *node;
16+
unsigned long int index;
17+
18+
if (ht == NULL || key == NULL || *key == '\0')
19+
return (NULL);
20+
21+
index = key_index((const unsigned char *)key, ht->size);
22+
if (index >= ht->size)
23+
return (NULL);
24+
25+
node = ht->array[index];
26+
while (node && strcmp(node->key, key) != 0)
27+
node = node->next;
28+
29+
return ((node == NULL) ? NULL : node->value);
30+
}

0x1A-hash_tables/5-hash_table_print.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#include "hash_tables.h"
3+
4+
/**
5+
* hash_table_print - Prints a hash table.
6+
* @ht: A pointer to the hash table to print.
7+
*
8+
* Description: Key/value pairs are printed in the order
9+
* they appear in the array of the hash table.
10+
*/
11+
void hash_table_print(const hash_table_t *ht)
12+
{
13+
hash_node_t *node;
14+
unsigned long int i;
15+
unsigned char comma_flag = 0;
16+
17+
if (ht == NULL)
18+
return;
19+
20+
printf("{");
21+
for (i = 0; i < ht->size; i++)
22+
{
23+
if (ht->array[i] != NULL)
24+
{
25+
if (comma_flag == 1)
26+
printf(", ");
27+
28+
node = ht->array[i];
29+
while (node != NULL)
30+
{
31+
printf("'%s': '%s'", node->key, node->value);
32+
node = node->next;
33+
if (node != NULL)
34+
printf(", ");
35+
}
36+
comma_flag = 1;
37+
}
38+
}
39+
printf("}\n");
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#include "hash_tables.h"
3+
4+
/**
5+
* hash_table_delete - Deletes a hash table.
6+
* @ht: A pointer to a hash table.
7+
*/
8+
void hash_table_delete(hash_table_t *ht)
9+
{
10+
hash_table_t *head = ht;
11+
hash_node_t *node, *tmp;
12+
unsigned long int i;
13+
14+
for (i = 0; i < ht->size; i++)
15+
{
16+
if (ht->array[i] != NULL)
17+
{
18+
node = ht->array[i];
19+
while (node != NULL)
20+
{
21+
tmp = node->next;
22+
free(node->key);
23+
free(node->value);
24+
free(node);
25+
node = tmp;
26+
}
27+
}
28+
}
29+
free(head->array);
30+
free(head);
31+
}

0 commit comments

Comments
 (0)