CHashTable 1
Loading...
Searching...
No Matches
CHashTable

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.

#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);
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:

./CHashTable

Run tests:

./CHashTable_tests

External tools used