-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-hash_table_set.c
More file actions
73 lines (68 loc) · 1.48 KB
/
3-hash_table_set.c
File metadata and controls
73 lines (68 loc) · 1.48 KB
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
#include "hash_tables.h"
/**
* make_hash_node - creates a new hash node
* @key: key for the node
* @value: for the node
*
* Return: the new node, or NULL on failure
*/
hash_node_t *make_hash_node(const char *key, const char *value)
{
hash_node_t *node;
node = malloc(sizeof(hash_node_t));
if (node == NULL)
return (NULL);
node->key = strdup(key);
if (node->key == NULL)
{
free(node);
return (NULL);
}
node->value = strdup(value);
if (node->value == NULL)
{
free(node->key);
free(node);
return (NULL);
}
node->next = NULL;
return (node);
}
/**
* hash_table_set - sets a key to a value in the hash table
* @ht: hash table to add elemt to
* @key: key for the data
* @value: data to store
*
* Return: 1 if successful, 0 otherwise
*/
int hash_table_set(hash_table_t *ht, const char *key, const char *value)
{
unsigned long int index;
hash_node_t *hash_node, *tmp;
char *new_value;
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;
}
hash_node = make_hash_node(key, value);
if (hash_node == NULL)
return (0);
hash_node->next = ht->array[index];
ht->array[index] = hash_node;
return (1);
}