From a874f2779bd8578203369903c775ecbf68795020 Mon Sep 17 00:00:00 2001 From: David Benavidez Date: Thu, 28 Feb 2019 13:22:37 -0800 Subject: [PATCH] hashtables commit -m --- basic_hashtable/b_hashtables.c | 50 ++++++++++++++++++++++++++-------- full_hashtable/hashtables.c | 29 ++++++++++++++++++-- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/basic_hashtable/b_hashtables.c b/basic_hashtable/b_hashtables.c index 7b813f68..57020409 100644 --- a/basic_hashtable/b_hashtables.c +++ b/basic_hashtable/b_hashtables.c @@ -45,7 +45,6 @@ void destroy_pair(Pair *pair) /**** djb2 hash function - Do not modify this! ****/ unsigned int hash(char *str, int max) @@ -64,57 +63,86 @@ unsigned int hash(char *str, int max) /**** Fill this in. - All values in storage should be initialized to NULL (hint: look up `calloc`) ****/ BasicHashTable *create_hash_table(int capacity) { - BasicHashTable *ht; + BasicHashTable *ht = malloc(sizeof(BasicHashTable)); + ht->storage = calloc(capacity, sizeof(Pair *)); + ht->capacity = capacity; return ht; } /**** Fill this in. - If you are overwriting a value with a different key, print a warning. - Don't forget to free any malloc'ed memory! ****/ void hash_table_insert(BasicHashTable *ht, char *key, char *value) { + // hash key + int hashedkey = hash(key, ht->capacity); + // make if key exists else make a pair with key and value given + if(ht->storage[hashedkey] != NULL) { + printf("Overwriting a value."); + // clear stored value + destroy_pair(ht->storage[hashedkey]); + // make new pair at given hashed index + ht->storage[hashedkey] = create_pair(key,value); + } + else { + // make new pair at given hashed index + ht->storage[hashedkey] = create_pair(key,value); + } } /**** Fill this in. - Don't forget to free any malloc'ed memory! ****/ void hash_table_remove(BasicHashTable *ht, char *key) { - + unsigned int hashedkey = hash(key, ht->capacity); + // if key even exists then clear ky value pair + if(ht->storage[hashedkey] != NULL) { + printf("Deleting a value."); + // clear stored value + destroy_pair(ht->storage[hashedkey]); + ht->storage[hashedkey] = NULL; + } } /**** Fill this in. - Should return NULL if the key is not found. ****/ char *hash_table_retrieve(BasicHashTable *ht, char *key) { + int hashedkey = hash(key, ht->capacity); + // check if value exists + if(ht->storage[hashedkey] != NULL) { + return ht->storage[hashedkey]->value; + } + return NULL; } /**** Fill this in. - Don't forget to free any malloc'ed memory! ****/ void destroy_hash_table(BasicHashTable *ht) { - + // loop through each bucket + for(int i =0; i < ht->capacity; i++) { + destroy_pair(ht->storage[i]); + } + // free malloc'ed memory + free(ht->storage); + free(ht); } @@ -139,4 +167,4 @@ int main(void) return 0; } -#endif +#endif \ No newline at end of file diff --git a/full_hashtable/hashtables.c b/full_hashtable/hashtables.c index 0dd61db9..81fcb10a 100644 --- a/full_hashtable/hashtables.c +++ b/full_hashtable/hashtables.c @@ -70,7 +70,9 @@ unsigned int hash(char *str, int max) ****/ HashTable *create_hash_table(int capacity) { - HashTable *ht; + HashTable *ht = malloc(sizeof(HashTable)); + ht->capacity = capacity; + ht->storage = calloc(capacity,sizeof(LinkedPair *)); return ht; } @@ -86,6 +88,29 @@ HashTable *create_hash_table(int capacity) ****/ void hash_table_insert(HashTable *ht, char *key, char *value) { + // Hash the key to get an array index + unsigned int index = hash(key, ht->capacity); + + // Check if the bucket at that index is occupied + LinkedPair *current_pair = ht->storage[index]; + LinkedPair *last_pair; + + // If it's not occupied, walk through the LinkedPairs to see if you find + // Check for a Pair with the same key using strcmp (string compare) + while (current_pair != NULL && strcmp(current_pair->key, key) != 0) { + last_pair = current_pair; + current_pair = last_pair->next; + } + if (current_pair != NULL) { + // If you do, overwrite that value + current_pair->value = value; + } else { + // If it's not occupied, add a new LinkedPair to the bucket + // If not, create a new pair and add it to the LinkedList + LinkedPair *new_pair = create_pair(key, value); + new_pair->next = ht->storage[index]; + ht->storage[index] = new_pair; + } } @@ -93,7 +118,7 @@ void hash_table_insert(HashTable *ht, char *key, char *value) Fill this in. Should search the entire list of LinkedPairs for existing - keys and remove matching LinkedPairs safely. + keys and remove matching LinkedPairs safely.ma Don't forget to free any malloc'ed memory! ****/