mirror of
				https://github.com/sloven-c/hashmap.git
				synced 2025-11-04 03:17:25 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			170 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			170 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include <stdio.h>
 | 
						|
#include <stdlib.h>
 | 
						|
 | 
						|
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;
 | 
						|
}
 |