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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- https://github.com/LambdaSchool/Hash-Tables/pull/151 -->
# Hash Tables

Hash tables are arguably the single most important data structures in existence. Used to implement everything from objects in JavaScript and dictionaries in Python to Memcached over a distributed computer network, hash tables are beloved by programmers for providing key/value storage with constant big-O time complexity for insertion, deletion, and access.
Expand Down
39 changes: 36 additions & 3 deletions basic_hashtable/b_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)
****/
BasicHashTable *create_hash_table(int capacity)
{
BasicHashTable *ht;
BasicHashTable *ht = malloc(sizeof(BasicHashTable));
ht->capacity = capacity;
ht->storage = calloc(capacity, sizeof(Pair *));

return ht;
}
Expand All @@ -84,6 +86,20 @@ BasicHashTable *create_hash_table(int capacity)
****/
void hash_table_insert(BasicHashTable *ht, char *key, char *value)
{
// create the pair to be inserted
Pair *insert_pair = create_pair(key, value);

// hash the value
int hashed = hash(key, ht->capacity);
Pair *current_pair = ht->storage[hashed];
if (ht->storage[hashed] != NULL) {
if (strcmp(current_pair->key, key) != 0) {
printf("warning!");
// handle collision tomorrow
}
destroy_pair(current_pair);
}
ht->storage[hashed] = insert_pair;

}

Expand All @@ -94,7 +110,12 @@ void hash_table_insert(BasicHashTable *ht, char *key, char *value)
****/
void hash_table_remove(BasicHashTable *ht, char *key)
{

int hashed = hash(key, ht->capacity);
Pair *current_pair = ht->storage[hashed];
if (ht->storage[hashed] != NULL) {
destroy_pair(current_pair);
ht->storage[hashed] = NULL;
}
}

/****
Expand All @@ -104,6 +125,11 @@ void hash_table_remove(BasicHashTable *ht, char *key)
****/
char *hash_table_retrieve(BasicHashTable *ht, char *key)
{
int hashed = hash(key, ht->capacity);
Pair *current_pair = ht->storage[hashed];
if (ht->storage[hashed] != NULL) {
return current_pair->value;
}
return NULL;
}

Expand All @@ -114,7 +140,14 @@ char *hash_table_retrieve(BasicHashTable *ht, char *key)
****/
void destroy_hash_table(BasicHashTable *ht)
{

for (int i = 0; i < ht->capacity; i++) {
if (ht->storage[i] != NULL) {
destroy_pair(ht->storage[i]);
ht->storage[i] = NULL;
}
}
free(ht->storage);
free(ht);
}


Expand Down
34 changes: 32 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 *));

return ht;
}
Expand All @@ -86,7 +88,22 @@ HashTable *create_hash_table(int capacity)
****/
void hash_table_insert(HashTable *ht, char *key, char *value)
{

LinkedPair *insert_pair = create_pair(key, value);

int hashed = hash(key, ht->capacity);

LinkedPair *current_pair = ht->storage[hashed];

if (ht->storage[hashed] != NULL) {
if (strcmp(current_pair->key, key) != 0) {
while (current_pair->next != NULL) {
current_pair = current_pair->next;
}
current_pair->next = insert_pair;
return;
}
}
ht->storage[hashed] = insert_pair;
}

/****
Expand All @@ -99,6 +116,19 @@ void hash_table_insert(HashTable *ht, char *key, char *value)
****/
void hash_table_remove(HashTable *ht, char *key)
{
int hashed = hash(key, ht->capacity);
LinkedPair *current_pair = ht->storage[hashed];
if (ht->storage[hashed] != NULL) {
while (current_pair->next != NULL) {
LinkedPair *next_pair = current_pair->next;
if (strcmp(current_pair->key, key) != 0) {
current_pair = current_pair->next;
}
// here they do match ---> now what
// even though they match, that doesnt mean the match->next is null
// need to point from previous element to next element
}
}

}

Expand Down