Kevin Sooter - Hash-Tables#147
Conversation
codejoncode
left a comment
There was a problem hiding this comment.
Great start Kevin. In your create_function ht->storage = calloc(capacity, sizeof(char *)) We need Pair struct pointers so ht->storage = calloc(capacity, sizeof(Pair *))
This is what you should have.
codejoncode
left a comment
There was a problem hiding this comment.
if (ht->storage[target_index] != NULL)
{ // If the node at the index is not empty
printf("You're overwriting an existing value value!\n"); // give warning
destroy_pair(ht->storage[target_index]); // free up the memory at that index
ht->storage[target_index] = new_pair; // insert new node into target index
}
Your condition has a good base we want to destroy the pair if its not null however we want to print out a error if we overwrite so we want to do a check for if (strcmp(ht->storage[target_index]->key, key) != 0) meaning if the key that is there is different than the key we receive in the function. If the key is the same then we are just performing a update if its different we are overwriting what's there. This would be a nested conditional inside of the one you already have.
No description provided.