This is a simple implementation of a hash table in C. The code provides functions for creating a hash table, inserting key-value pairs, retrieving values by key, and freeing the allocated memory.
-
Hash function for mapping keys to buckets.
-
Collision handling using separate chaining (linked lists).
-
Functions for inserting, retrieving, and freeing memory.
-
Simple demonstration with example key-value pairs.
-
Clone the repository to your local machine:
git clone https://github.com/mdawoud27/data_structures_and_algorithms.git
-
Navigate to the project directory
hash_tables
-
Compile the code using GCC:
gcc -o hash main.c
To use this hash table implementation, follow these steps:
-
Include the necessary headers at the beginning of your C file:
#include <stdio.h> #include <stdlib.h> #include <string.h>
-
Define the hash table and hash functions:
#define TABLE_SIZE 100 // Structures and functions as provided in the code
-
Create a hash table:
hash_table_t *table = create_table(TABLE_SIZE);
-
Insert key-value pairs:
insert(table, "name", "dawoud"); insert(table, "age", "21"); insert(table, "location", "ALX"); insert(table, "apple", "red"); insert(table, "banana", "yellow"); insert(table, "cherry", "red");
-
Retrieve values by key:
char *value = get(table, "banana"); printf("Value for key 'banana': %s\n", value);
-
Print the contents of the hash table:
print_table(table);
-
Clean up and free memory:
free_table(table);
Contributions are welcome! If you'd like to improve this code or add new features, please fork the repository and create a pull request.
Feel free to customize this README
file to better suit your project's needs. You can add more sections, provide additional information, and include any relevant documentation.