Skip to content

Commit 48788cd

Browse files
committed
add last tasks
1 parent 0950ffe commit 48788cd

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

0x1A-hash_tables/2-key_index.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* key_index - Get the index at which a key/value
5+
* pair should be stored in array of a hash table.
6+
* @key: The key to get the index of.
7+
* @size: The size of the array of the hash table.
8+
*
9+
* Return: The index of the key.
10+
* Description: Uses the djb2 algorithm.
11+
*/
12+
unsigned long int key_index(const unsigned char *key, unsigned long int size)
13+
{
14+
return (hash_djb2(key) % size);
15+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_table_set - Add or update an element in a hash table.
5+
* @ht: A pointer to the hash table.
6+
* @key: The key to add - cannot be an empty string.
7+
* @value: The value associated with key.
8+
*
9+
* Return: Upon failure - 0.
10+
* Otherwise - 1.
11+
*/
12+
int hash_table_set(hash_table_t *ht, const char *key, const char *value)
13+
{
14+
hash_node_t *new;
15+
char *value_copy;
16+
unsigned long int index, i;
17+
18+
if (ht == NULL || key == NULL || *key == '\0' || value == NULL)
19+
return (0);
20+
21+
value_copy = strdup(value);
22+
if (value_copy == NULL)
23+
return (0);
24+
25+
index = key_index((const unsigned char *)key, ht->size);
26+
for (i = index; ht->array[i]; i++)
27+
{
28+
if (strcmp(ht->array[i]->key, key) == 0)
29+
{
30+
free(ht->array[i]->value);
31+
ht->array[i]->value = value_copy;
32+
return (1);
33+
}
34+
}
35+
36+
new = malloc(sizeof(hash_node_t));
37+
if (new == NULL)
38+
{
39+
free(value_copy);
40+
return (0);
41+
}
42+
new->key = strdup(key);
43+
if (new->key == NULL)
44+
{
45+
free(new);
46+
return (0);
47+
}
48+
new->value = value_copy;
49+
new->next = ht->array[index];
50+
ht->array[index] = new;
51+
52+
return (1);
53+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_table_get - Retrieve the value associated with
5+
* a key in a hash table.
6+
* @ht: A pointer to the hash table.
7+
* @key: The key to get the value of.
8+
*
9+
* Return: If the key cannot be matched - NULL.
10+
* Otherwise - the value associated with key in ht.
11+
*/
12+
char *hash_table_get(const hash_table_t *ht, const char *key)
13+
{
14+
hash_node_t *node;
15+
unsigned long int index;
16+
17+
if (ht == NULL || key == NULL || *key == '\0')
18+
return (NULL);
19+
20+
index = key_index((const unsigned char *)key, ht->size);
21+
if (index >= ht->size)
22+
return (NULL);
23+
24+
node = ht->array[index];
25+
while (node && strcmp(node->key, key) != 0)
26+
node = node->next;
27+
28+
return ((node == NULL) ? NULL : node->value);
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_table_print - Prints a hash table.
5+
* @ht: A pointer to the hash table to print.
6+
*
7+
* Description: Key/value pairs are printed in the order
8+
* they appear in the array of the hash table.
9+
*/
10+
void hash_table_print(const hash_table_t *ht)
11+
{
12+
hash_node_t *node;
13+
unsigned long int i;
14+
unsigned char comma_flag = 0;
15+
16+
if (ht == NULL)
17+
return;
18+
19+
printf("{");
20+
for (i = 0; i < ht->size; i++)
21+
{
22+
if (ht->array[i] != NULL)
23+
{
24+
if (comma_flag == 1)
25+
printf(", ");
26+
27+
node = ht->array[i];
28+
while (node != NULL)
29+
{
30+
printf("'%s': '%s'", node->key, node->value);
31+
node = node->next;
32+
if (node != NULL)
33+
printf(", ");
34+
}
35+
comma_flag = 1;
36+
}
37+
}
38+
printf("}\n");
39+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_table_delete - Deletes a hash table.
5+
* @ht: A pointer to a hash table.
6+
*/
7+
void hash_table_delete(hash_table_t *ht)
8+
{
9+
hash_table_t *head = ht;
10+
hash_node_t *node, *tmp;
11+
unsigned long int i;
12+
13+
for (i = 0; i < ht->size; i++)
14+
{
15+
if (ht->array[i] != NULL)
16+
{
17+
node = ht->array[i];
18+
while (node != NULL)
19+
{
20+
tmp = node->next;
21+
free(node->key);
22+
free(node->value);
23+
free(node);
24+
node = tmp;
25+
}
26+
}
27+
}
28+
free(head->array);
29+
free(head);
30+
}

0 commit comments

Comments
 (0)