Skip to content

Commit 57f4d22

Browse files
committed
Add mandatory tasks
1 parent 3a7c262 commit 57f4d22

13 files changed

+162
-0
lines changed

0x1A-hash_tables/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
.vscode/*
3+
tests.c
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_table_create - creates a hash table.
5+
*
6+
* @size: Size of the array.
7+
*
8+
* Return: pointer to a hash table, NULL otherwise.
9+
*/
10+
hash_table_t *hash_table_create(unsigned long int size)
11+
{
12+
hash_table_t *hash_table = malloc(sizeof(hash_node_t));
13+
unsigned int index;
14+
15+
if (hash_table == NULL)
16+
return (NULL);
17+
18+
hash_table->size = size;
19+
hash_table->array = malloc(sizeof(hash_node_t *) * size);
20+
if (hash_table->array == NULL)
21+
{
22+
free(hash_table);
23+
return (NULL);
24+
}
25+
26+
for (index = 0; index < size; index++)
27+
hash_table->array[index] = NULL;
28+
29+
return (hash_table);
30+
}

0x1A-hash_tables/0-main.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* main - check the code for
5+
*
6+
* Return: Always EXIT_SUCCESS.
7+
*/
8+
int main(void)
9+
{
10+
hash_table_t *ht;
11+
12+
ht = hash_table_create(1024);
13+
printf("%p\n", (void *)ht);
14+
return (EXIT_SUCCESS);
15+
}

0x1A-hash_tables/1-djb2.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* hash_djb2 - hash function (djb2 algorithm)
5+
*
6+
* @str: The key to be hashed
7+
*
8+
* Return: The hash
9+
*/
10+
unsigned long int hash_djb2(const unsigned char *str)
11+
{
12+
unsigned long int hash;
13+
int c;
14+
15+
hash = 5381;
16+
while ((c = *str++))
17+
{
18+
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
19+
}
20+
return (hash);
21+
}

0x1A-hash_tables/1-main.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "hash_tables.h"
2+
3+
/**
4+
* main - check the code
5+
*
6+
* Return: Always EXIT_SUCCESS.
7+
*/
8+
int main(void)
9+
{
10+
char *s;
11+
12+
s = "cisfun";
13+
printf("%lu\n", hash_djb2((unsigned char *)s));
14+
s = "Don't forget to tweet today";
15+
printf("%lu\n", hash_djb2((unsigned char *)s));
16+
s = "98";
17+
printf("%lu\n", hash_djb2((unsigned char *)s));
18+
return (EXIT_SUCCESS);
19+
}

0x1A-hash_tables/100-sorted_hash_table.c

Whitespace-only changes.

0x1A-hash_tables/2-key_index.c

Whitespace-only changes.

0x1A-hash_tables/3-hash_table_set.c

Whitespace-only changes.

0x1A-hash_tables/4-hash_table_get.c

Whitespace-only changes.

0x1A-hash_tables/5-hash_table_print.c

Whitespace-only changes.

0x1A-hash_tables/6-hash_table_delete.c

Whitespace-only changes.

0x1A-hash_tables/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Project: 0x1A. C - Hash tables
2+
3+
## Resources
4+
5+
#### Read or watch:
6+
7+
* [What is a HashTable Data Structure - Introduction to Hash Tables , Part 0](https://intranet.alxswe.com/rltoken/IQVfdxJlS6jhAgcuUoCseg)
8+
* [Hash function](https://intranet.alxswe.com/rltoken/ZKpRI_FxOxAz80Onpfy0Ew)
9+
* [Hash table](https://intranet.alxswe.com/rltoken/mxjKpEfAw3E5B8S3inPuHQ)
10+
* [All about hash tables](https://intranet.alxswe.com/rltoken/3RwwAqmpGJpMiBa7BE9fAQ)
11+
* [why hash tables and not arrays](https://intranet.alxswe.com/rltoken/OgO7uga3PIaCTMtTzYCY3g)
12+
## Learning Objectives
13+
14+
### General
15+
16+
* What is a hash function
17+
* What makes a good hash function
18+
* What is a hash table, how do they work and how to use them
19+
* What is a collision and what are the main ways of dealing with collisions in the context of a hash table
20+
* What are the advantages and drawbacks of using hash tables
21+
* What are the most common use cases of hash tables
22+
## Tasks
23+
24+
| Task | File |
25+
| ---- | ---- |
26+
| 0. >>> ht = {} | [0-hash_table_create.c](./0-hash_table_create.c) |
27+
| 1. djb2 | [1-djb2.c](./1-djb2.c) |
28+
| 2. key -> index | [2-key_index.c](./2-key_index.c) |
29+
| 3. >>> ht['betty'] = 'cool' | [3-hash_table_set.c](./3-hash_table_set.c) |
30+
| 4. >>> ht['betty'] | [4-hash_table_get.c](./4-hash_table_get.c) |
31+
| 5. >>> print(ht) | [5-hash_table_print.c](./5-hash_table_print.c) |
32+
| 6. >>> del ht | [6-hash_table_delete.c](./6-hash_table_delete.c) |
33+
| 7. $ht['Betty'] = 'Cool' | [100-sorted_hash_table.c](./100-sorted_hash_table.c) |

0x1A-hash_tables/hash_tables.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef HASH_TABLES
2+
#define HASH_TABLES
3+
4+
#include <stdlib.h>
5+
#include <stdio.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+
40+
41+
#endif /* HASH_TABLES */

0 commit comments

Comments
 (0)