Skip to content

Commit 3123ac9

Browse files
committed
6-hash_table_delete.c
1 parent 4340887 commit 3123ac9

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_table_delete - short description
5+
* Description: long description
6+
* @ht: argument_1 description
7+
* Return: return description
8+
*/
9+
10+
void hash_table_delete(hash_table_t *ht)
11+
{
12+
hash_table_t *head = ht;
13+
hash_node_t *node, *tmp;
14+
unsigned long int i;
15+
16+
for (i = 0; i < ht->size; i++)
17+
{
18+
if (ht->array[i] != NULL)
19+
{
20+
node = ht->array[i];
21+
while (node != NULL)
22+
{
23+
tmp = node->next;
24+
free(node->key);
25+
free(node->value);
26+
free(node);
27+
node = tmp;
28+
}
29+
}
30+
}
31+
free(head->array);
32+
free(head);
33+
}

0x1A-hash_tables/6-main.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include "hash_tables.h"
5+
6+
/**
7+
* main - check the code
8+
*
9+
* Return: Always EXIT_SUCCESS.
10+
*/
11+
int main(void)
12+
{
13+
hash_table_t *ht;
14+
char *key;
15+
char *value;
16+
17+
ht = hash_table_create(1024);
18+
hash_table_set(ht, "c", "fun");
19+
hash_table_set(ht, "python", "awesome");
20+
hash_table_set(ht, "Bob", "and Kris love asm");
21+
hash_table_set(ht, "N", "queens");
22+
hash_table_set(ht, "Asterix", "Obelix");
23+
hash_table_set(ht, "Betty", "Cool");
24+
hash_table_set(ht, "98", "Battery Streetz");
25+
key = strdup("Tim");
26+
value = strdup("Britton");
27+
hash_table_set(ht, key, value);
28+
key[0] = '\0';
29+
value[0] = '\0';
30+
free(key);
31+
free(value);
32+
hash_table_set(ht, "98", "Battery Street");
33+
hash_table_set(ht, "hetairas", "Bob");
34+
hash_table_set(ht, "hetairas", "Bob Z");
35+
hash_table_set(ht, "mentioner", "Bob");
36+
hash_table_set(ht, "hetairas", "Bob Z Chu");
37+
hash_table_print(ht);
38+
hash_table_delete(ht);
39+
return (EXIT_SUCCESS);
40+
}

0x1A-hash_tables/g

17.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)