Hashtable implemented in C for a university project.
Implementation
A simple hash table implementation in C with integer keys and values, using linked list chaining for collisions and dynamic resizing for performance.
Read detailed specification.
Docs
Example usage
To use the hash table in a project include the .c and .h files from the src directory. Except main.c. Then include the hash_table.h public API header.
HashTable *table = hash_table_create();
hash_table_insert(table, 1, 10);
printf(
"Key: %d\n",
entry->key);
printf(
"Value: %d\n",
entry->value);
bool success = hash_table_delete(table, 1);
HashTable *copy = hash_table_copy(table);
bool is_equal = hash_table_equal(table, copy);
void print_all_key_value_pairs(int key, int value, void *user_data) {
printf("Key: %d\n", key);
printf("Value: %d\n", value);
}
hash_table_foreach(table, print_all_key_value_pairs, nullptr);
hash_table_save(table, "hash_table.txt");
hash_table_print(table, false);
hash_table_destroy(table);
Public API for HashTable.
HashTable_LoadError hash_table_load(const char *filename, HashTable **out_table)
Loads a hash table from a specified file.
Definition hash_table_io.c:42
A hash table entry object.
Definition hash_table.h:20
Compile
Compile using CMake.
Compile:
mkdir build
cd build
cmake ..
make
Run interactive mode:
Run tests:
External tools used
- Tests use the µnit framework
- Docs generated with Doxygen