Skip to content

Commit 1baf745

Browse files
author
Shell-thon
committed
update
1 parent 5635c95 commit 1baf745

File tree

5 files changed

+143
-87
lines changed

5 files changed

+143
-87
lines changed

0x1A-hash_tables/100-main.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include "hash_tables.h"
5+
6+
/**
7+
* main - check the code for Holberton School students.
8+
*
9+
* Return: Always EXIT_SUCCESS.
10+
*/
11+
int main(void)
12+
{
13+
shash_table_t *ht;
14+
char *value, *key;
15+
16+
ht = shash_table_create(1024);
17+
shash_table_set(ht, "y", "0");
18+
shash_table_print(ht);
19+
shash_table_set(ht, "j", "1");
20+
shash_table_print(ht);
21+
shash_table_set(ht, "c", "2");
22+
shash_table_print(ht);
23+
shash_table_set(ht, "b", "3");
24+
shash_table_print(ht);
25+
shash_table_set(ht, "z", "4");
26+
shash_table_print(ht);
27+
shash_table_set(ht, "n", "5");
28+
shash_table_print(ht);
29+
shash_table_set(ht, "a", "6");
30+
shash_table_print(ht);
31+
shash_table_set(ht, "m", "7");
32+
shash_table_print(ht);
33+
shash_table_print_rev(ht);
34+
35+
36+
key = "y";
37+
value = shash_table_get(ht, key);
38+
printf("%s:%s\n", key, value);
39+
40+
shash_table_delete(ht);
41+
return (EXIT_SUCCESS);
42+
}

0x1A-hash_tables/3-hash_table_set.c

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,53 @@
11
#include "hash_tables.h"
22

33
/**
4-
* set_pair - mallocs a key/value pair to the hash table.
5-
* @node: a pointer to the hash table array node.
6-
* @key: the key, a string that cannot be empty.
7-
* @value: the value associated with the key, can be an empty string.
4+
* hash_table_set - adds an element to the hash table
5+
* @ht: hash table
6+
* @key: key for hash table
7+
* @value: value for key
88
*
9-
* Return: 1 on success, 0 on error.
10-
*/
11-
int set_pair(hash_node_t **node, const char *key, const char *value)
12-
{
13-
*node = malloc(sizeof(hash_node_t));
14-
if (*node == NULL)
15-
return (0);
16-
(*node)->key = malloc(strlen(key) + 1);
17-
if ((*node)->key == NULL)
18-
return (0);
19-
(*node)->value = malloc(strlen(value) + 1);
20-
if ((*node)->value == NULL)
21-
return (0);
22-
strcpy((*node)->key, key);
23-
strcpy((*node)->value, value);
24-
return (1);
25-
}
26-
27-
/**
28-
* hash_table_set - adds an element to the hash table.
29-
* @ht: a pointer to the hash table array.
30-
* @key: the key, a string that cannot be empty.
31-
* @value: the value associated with the key, can be an empty string.
32-
*
33-
* Return: 1 on success, 0 on error.
9+
* Return: 1 if it succeeded, 0 otherwise
3410
*/
3511
int hash_table_set(hash_table_t *ht, const char *key, const char *value)
3612
{
13+
hash_node_t *hnode = NULL, *collnode = NULL;
3714
unsigned long int index;
38-
hash_node_t *node;
15+
char *valuedup = NULL, *keydup = NULL;
3916

40-
if (key == NULL)
17+
if (!ht || !(ht->array) || !key || !key[0])
4118
return (0);
19+
4220
index = key_index((unsigned char *)key, ht->size);
43-
node = ht->array[index];
44-
if (node == NULL)
45-
{
46-
if (set_pair(&node, key, value) == 0)
47-
return (0);
48-
node->next = NULL;
49-
return (1);
50-
}
51-
while (node != NULL)
21+
hnode = ht->array[index];
22+
valuedup = strdup(value);
23+
if (!valuedup)
24+
return (0);
25+
26+
while (hnode != NULL)
5227
{
53-
if (strcmp(node->key, key) == 0)
28+
if (!strcmp(hnode->key, key))
5429
{
55-
if (strcmp(node->value, value) == 0)
56-
return (1);
57-
free(node->value);
58-
node->value = malloc(strlen(value) + 1);
59-
if (node->value == NULL)
60-
return (0);
61-
strcpy(node->value, value);
30+
free(hnode->value);
31+
hnode->value = valuedup;
6232
return (1);
6333
}
64-
node = node->next;
34+
hnode = hnode->next;
6535
}
66-
if (node == NULL)
67-
{
68-
if (set_pair(&node, key, value) == 0)
69-
return (0);
70-
node->next = ht->array[index];
71-
ht->array[index] = node;
72-
return (1);
73-
}
74-
return (0);
36+
37+
hnode = ht->array[index];
38+
39+
collnode = malloc(sizeof(hash_node_t));
40+
if (collnode == NULL)
41+
return (free(valuedup), 0);
42+
43+
keydup = strdup(key);
44+
if (!keydup)
45+
return (free(valuedup), free(collnode), 0);
46+
47+
collnode->key = keydup;
48+
collnode->value = valuedup;
49+
collnode->next = hnode;
50+
ht->array[index] = collnode;
51+
52+
return (1);
7553
}

0x1A-hash_tables/4-hash_table_get.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
#include "hash_tables.h"
22

33
/**
4-
* hash_table_get - retrieves a value associated with a key.
5-
* @ht: a pointer to the hash table array.
6-
* @key: the key, a string that cannot be empty.
4+
* hash_table_get - retrieves a value associated with a key
5+
* @ht: hash table
6+
* @key: key for retrieving
77
*
8-
* Return: the value associated with the key, or NULL if key can't be found.
8+
* Return: 1 if it succeeded, 0 otherwise
99
*/
1010
char *hash_table_get(const hash_table_t *ht, const char *key)
1111
{
12+
hash_node_t *hnode = NULL;
1213
unsigned long int index;
13-
hash_node_t *node;
1414

15-
if (ht == NULL)
16-
return (NULL);
17-
if (key == NULL)
15+
if (!ht || !key || !key[0])
1816
return (NULL);
17+
1918
index = key_index((unsigned char *)key, ht->size);
20-
if (ht->array[index] == NULL)
21-
return (NULL);
22-
if (strcmp(ht->array[index]->key, key) == 0)
23-
return (ht->array[index]->value);
24-
node = ht->array[index];
25-
while (node != NULL)
19+
hnode = ht->array[index];
20+
21+
while (hnode != NULL)
2622
{
27-
if (strcmp(node->key, key) == 0)
28-
return (node->value);
29-
node = node->next;
23+
if (!strcmp(hnode->key, key))
24+
return (hnode->value);
25+
26+
hnode = hnode->next;
3027
}
28+
3129
return (NULL);
3230
}

0x1A-hash_tables/5-hash_table_print.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_table_print - prints a hash table
5+
* @ht: hash table
6+
*
7+
* Return: No Return
8+
*/
9+
void hash_table_print(const hash_table_t *ht)
10+
{
11+
hash_node_t *hnode;
12+
int comma = 0;
13+
unsigned long int i;
14+
15+
if (!ht)
16+
return;
17+
18+
printf("{");
19+
for (i = 0; i < ht->size; i++)
20+
{
21+
hnode = ht->array[i];
22+
while (hnode)
23+
{
24+
if (comma)
25+
printf(", ");
26+
printf("'%s': '%s'", hnode->key, hnode->value);
27+
hnode = hnode->next;
28+
comma = 1;
29+
}
30+
}
31+
printf("}\n");
32+
}

0x1A-hash_tables/6-hash_table_delete.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
#include "hash_tables.h"
22

33
/**
4-
* hash_table_delete - deletes a hash table.
5-
* @ht: the hash table.
4+
* hash_table_delete - deletes a hash table
5+
* @ht: hash table
6+
*
7+
* Return: No Return
8+
*
69
*/
710
void hash_table_delete(hash_table_t *ht)
811
{
9-
unsigned long int i = 0;
10-
hash_node_t *node;
12+
hash_node_t *hnode, *tmp;
13+
unsigned long int i;
1114

12-
while (i < ht->size)
15+
if (!ht)
16+
return;
17+
18+
for (i = 0; i < ht->size; i++)
1319
{
14-
while (ht->array[i] != NULL)
20+
hnode = ht->array[i];
21+
while (hnode)
1522
{
16-
node = ht->array[i]->next;
17-
free(ht->array[i]->key);
18-
free(ht->array[i]->value);
19-
free(ht->array[i]);
20-
ht->array[i] = node;
23+
tmp = hnode->next;
24+
free(hnode->key);
25+
free(hnode->value);
26+
free(hnode);
27+
hnode = tmp;
2128
}
22-
i++;
2329
}
2430
free(ht->array);
2531
free(ht);

0 commit comments

Comments
 (0)