From 6a3e59ffeb0234a67854b804b4bee5ba2fb2909c Mon Sep 17 00:00:00 2001 From: sloven-c Date: Tue, 21 Oct 2025 11:41:43 +0200 Subject: [PATCH] Initial commit --- CMakeLists.txt | 6 ++ main.c | 169 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..28bdddd --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 4.0) +project(hashmap C) + +set(CMAKE_C_STANDARD 23) + +add_executable(hashmap main.c) diff --git a/main.c b/main.c new file mode 100644 index 0000000..c736dbc --- /dev/null +++ b/main.c @@ -0,0 +1,169 @@ +#include +#include + +struct HashMap { + char *key, *value; + struct HashMap *next; +}; + +struct HashMap* init(); +void deinit(struct HashMap *hashmap); +int insert(struct HashMap *hashmap, char *key, char *value); +int edit(struct HashMap *hashmap, const char *key, char *value); +int rem(struct HashMap **hashmap, const char *key); +char* get(const struct HashMap *hashmap, const char *key); +void print_all(const struct HashMap *hashmap); +int maplen(const struct HashMap *hashmap); + +int main() { + struct HashMap *dict = init(); + insert(dict, "russia", "east"); + insert(dict, "india", "south"); + insert(dict, "slovenia", "center"); + + printf("Initial dictionary size: %d\n\n", maplen(dict)); + + printf("Printing all pairs:\n"); + print_all(dict); + printf("\n"); + + const char *key = "russia"; + const char *value = get(dict, key); + if (value != NULL) printf("'%s' => '%s'\n\n", key, value); + + const int res = rem(&dict, key); + if (!res) { + printf("Removal of key '%s' succeded. Current size %d\n", key, maplen(dict)); + } else { + printf("Failed to remove the key '%s'\n", key); + } + + const char *ekey = "india"; + char *eval = "bharat"; + printf("Attempting to change '%s' => '%s'\n", ekey, eval); + int eres = edit(dict, ekey, eval); + if (!eres) { + printf("Change successful!\n"); + } else { + printf("Change failed!\n"); + } + printf("\n"); + + printf("Printing all pairs:\n"); + print_all(dict); + printf("\n"); + + deinit(dict); + return 0; +} + +struct HashMap* init() { + struct HashMap* hashmap = malloc(sizeof(struct HashMap)); + if (hashmap == NULL) { + fprintf(stderr, "Failed to allocate the memory to initialise hashmap"); + exit(1); + } + hashmap->key = nullptr; + hashmap->value = nullptr; + hashmap->next = nullptr; + + return hashmap; +} + +void deinit(struct HashMap *hashmap) { + if (hashmap == NULL) { + return; + } + + deinit(hashmap->next); + free(hashmap); +} + +int insert(struct HashMap *hashmap, char *key, char *value) { + if (get(hashmap, key) != NULL) return 1; + bool found_null = false; + + for (struct HashMap *it = hashmap; it != NULL; it = it->next) { + if (it->next == NULL) { + hashmap = it; + } + + if (it->key == NULL && it->value == NULL) { + hashmap = it; + found_null = true; + break; // we found an empty space + } + } + + if (found_null) { + hashmap->key = key; + hashmap->value = value; + return 0; + } + + struct HashMap *new_map = init(); + new_map->key = key; + new_map->value = value; + hashmap->next = new_map; + + return 0; +} + +int edit(struct HashMap *hashmap, const char *key, char *value) { + for (; hashmap != NULL; hashmap = hashmap->next) { + if (hashmap->key == key) { + hashmap->value = value; + return 0; + } + } + + return 1; +} + +int rem(struct HashMap **hashmap, const char *key) { + struct HashMap *it = *hashmap; + struct HashMap *prev = nullptr; + + for (; it != NULL; it = it->next) { + if (it->key == key) { + // reassign prev pointer to next + if (prev != NULL) { + prev->next = it->next == NULL ? nullptr : it->next; + } else { + *hashmap = it->next; // set the caller pointer to point at the non-null element + } + // clear the pointer + free(it); + return 0; + } + prev = it; + } + + return 1; // didn't find the pair +} + +char* get(const struct HashMap *hashmap, const char *key) { + for (; hashmap != NULL; hashmap = hashmap->next) { + if (hashmap->key == key) { + return hashmap->value; + } + } + + return nullptr; +} + +void print_all(const struct HashMap *hashmap) { + for (; hashmap != NULL; hashmap = hashmap->next) { + printf("'%s': '%s'\n", hashmap->key, hashmap->value); + } +} + +int maplen(const struct HashMap *hashmap) { + int len = 0; + + for (; hashmap != NULL; hashmap = hashmap->next) { + len++; + } + + return len; +}