Skip to content

Commit c27afc4

Browse files
committed
0-hash_table_create.c and header file added
1 parent a76a249 commit c27afc4

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_table_create - Creates a hash table.
5+
* @size: The size of the array.
6+
*
7+
* Return: If an error occurs - NULL.
8+
* Otherwise - a pointer to the new hash table.
9+
*/
10+
hash_table_t *hash_table_create(unsigned long int size)
11+
{
12+
hash_table_t *ht;
13+
unsigned long int i;
14+
15+
ht = malloc(sizeof(hash_table_t));
16+
if (ht == NULL)
17+
return (NULL);
18+
19+
ht->size = size;
20+
ht->array = malloc(sizeof(hash_node_t *) * size);
21+
if (ht->array == NULL)
22+
return (NULL);
23+
for (i = 0; i < size; i++)
24+
ht->array[i] = NULL;
25+
26+
return (ht);
27+
}

0x1A-hash_tables/hash_tables.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifndef HASH_TABLES_H
2+
#define HASH_TABLES_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
/**
9+
* struct hash_node_s - Node of a hash table
10+
*
11+
* @key: The key, string
12+
* The key is unique in the HashTable
13+
* @value: The value corresponding to a key
14+
* @next: A pointer to the next node of the List
15+
*/
16+
typedef struct hash_node_s
17+
{
18+
char *key;
19+
char *value;
20+
struct hash_node_s *next;
21+
} hash_node_t;
22+
23+
/**
24+
* struct hash_table_s - Hash table data structure
25+
*
26+
* @size: The size of the array
27+
* @array: An array of size @size
28+
* Each cell of this array is a pointer to the first node of a linked list,
29+
* because we want our HashTable to use a Chaining collision handling
30+
*/
31+
typedef struct hash_table_s
32+
{
33+
unsigned long int size;
34+
hash_node_t **array;
35+
} hash_table_t;
36+
37+
hash_table_t *hash_table_create(unsigned long int size);
38+
unsigned long int hash_djb2(const unsigned char *str);
39+
unsigned long int key_index(const unsigned char *key, unsigned long int size);
40+
int hash_table_set(hash_table_t *ht, const char *key, const char *value);
41+
char *hash_table_get(const hash_table_t *ht, const char *key);
42+
void hash_table_print(const hash_table_t *ht);
43+
void hash_table_delete(hash_table_t *ht);
44+
45+
/**
46+
* struct shash_node_s - Node of a sorted hash table
47+
*
48+
* @key: The key, string
49+
* The key is unique in the HashTable
50+
* @value: The value corresponding to a key
51+
* @next: A pointer to the next node of the List
52+
* @sprev: A pointer to the previous element of the sorted linked list
53+
* @snext: A pointer to the next element of the sorted linked list
54+
*/
55+
typedef struct shash_node_s
56+
{
57+
char *key;
58+
char *value;
59+
struct shash_node_s *next;
60+
struct shash_node_s *sprev;
61+
struct shash_node_s *snext;
62+
} shash_node_t;
63+
64+
/**
65+
* struct shash_table_s - Sorted hash table data structure
66+
*
67+
* @size: The size of the array
68+
* @array: An array of size @size
69+
* Each cell of this array is a pointer to the first node of a linked list,
70+
* because we want our HashTable to use a Chaining collision handling
71+
* @shead: A pointer to the first element of the sorted linked list
72+
* @stail: A pointer to the last element of the sorted linked list
73+
*/
74+
typedef struct shash_table_s
75+
{
76+
unsigned long int size;
77+
shash_node_t **array;
78+
shash_node_t *shead;
79+
shash_node_t *stail;
80+
} shash_table_t;
81+
82+
shash_table_t *shash_table_create(unsigned long int size);
83+
int shash_table_set(shash_table_t *ht, const char *key, const char *value);
84+
char *shash_table_get(const shash_table_t *ht, const char *key);
85+
void shash_table_print(const shash_table_t *ht);
86+
void shash_table_print_rev(const shash_table_t *ht);
87+
void shash_table_delete(shash_table_t *ht);
88+
89+
#endif /* HASH_TABLES */

0 commit comments

Comments
 (0)