Hashtable implemented in C for a university project.
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.
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.
#include "hash_table.h"
// Create HashTable
HashTable *table = hash_table_create();
// Insert key-value pair
hash_table_insert(table, 1, 10);
// Get entry
const Entry *entry = hash_table_get(table, 1);
printf("Key: %d\n", entry->key);
printf("Value: %d\n", entry->value);
// Delete entry
bool success = hash_table_delete(table, 1);
// Copy table
HashTable *copy = hash_table_copy(table);
// Check for equality
bool is_equal = hash_table_equal(table, copy); // true
// Foreach
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);
// Save to file
hash_table_save(table, "hash_table.txt");
// Load from file
HashTable *loaded_table = hash_table_load("hash_table.txt");
// Print for debugging
hash_table_print(table, false);
// Free HashTable
hash_table_destroy(table);Compile using CMake.
Compile:
mkdir build
cd build
cmake ..
makeRun interactive mode:
./CHashTableRun tests:
./CHashTable_tests