Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions basic_hashtable/b_hashtables.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void destroy_pair(Pair *pair)

/****
djb2 hash function

Do not modify this!
****/
unsigned int hash(char *str, int max)
Expand All @@ -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 *));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is 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]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is strictly necessary, but it doesn't hurt.

// 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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good job checking if this is not 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;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!


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]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop is spot on!

}
// free malloc'ed memory
free(ht->storage);
free(ht);
}


Expand All @@ -139,4 +167,4 @@ int main(void)

return 0;
}
#endif
#endif
29 changes: 27 additions & 2 deletions full_hashtable/hashtables.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is sizeof(LinkedPair). If you wanted the size of the pointer, it would be sizeof(*LinkedPair).


return ht;
}
Expand All @@ -86,14 +88,37 @@ 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;
}

}

/****
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!
****/
Expand Down