rewrite functions to work with updated strucutres

This commit is contained in:
Emanuel Rodriguez 2023-08-28 00:03:47 -07:00
parent e819633ba0
commit f3b0be9500
1 changed files with 44 additions and 18 deletions

View File

@ -7,23 +7,33 @@
typedef struct KV { typedef struct KV {
char *key; char *key;
char *value; char *value;
KV data[MAX_HASH_TABLE_SIZE];
} KV; } KV;
typedef struct Node {
KV kv;
struct Node *next;
} Node;
typedef struct HashTable { typedef struct HashTable {
KV data[MAX_HASH_TABLE_SIZE]; Node *data[MAX_HASH_TABLE_SIZE];
} HashTable; } HashTable;
HashTable *new_hashtable() { HashTable *new_hashtable() {
HashTable *ht = malloc(sizeof(HashTable)); HashTable *ht = malloc(sizeof(HashTable));
for (int i = 0; i < MAX_HASH_TABLE_SIZE; i++) { for (int i = 0; i < MAX_HASH_TABLE_SIZE; i++) {
ht->data[i].key = NULL; ht->data[i] = NULL;
ht->data[i].value = NULL;
} }
return (ht); return (ht);
} }
Node *new_node() {
Node *node = malloc(sizeof(Node));
node->next = NULL;
return (node);
}
int get_index_for_letter(char letter) { int get_index_for_letter(char letter) {
if (letter >= 'a' && letter <= 'z') { if (letter >= 'a' && letter <= 'z') {
return (letter - 'a') + 1; return (letter - 'a') + 1;
@ -46,8 +56,8 @@ int hash(char *word) {
KV *find_KV_in_ht(HashTable h, char *key) { KV *find_KV_in_ht(HashTable h, char *key) {
for (size_t i = 0; i < MAX_HASH_TABLE_SIZE; i++) { for (size_t i = 0; i < MAX_HASH_TABLE_SIZE; i++) {
if ((h.data[i].key != NULL) && (strcmp(h.data[i].key, key) == 0)) { if ((h.data[i] != NULL) && (strcmp(h.data[i]->kv.key, key) == 0)) {
return (&h.data[i]); return (&h.data[i]->kv);
} }
} }
@ -55,25 +65,42 @@ KV *find_KV_in_ht(HashTable h, char *key) {
} }
char *get_value(HashTable ht, char *key) { char *get_value(HashTable ht, char *key) {
KV *kv = find_KV_in_ht(ht, key); int hash_key = hash(key);
Node *node_at_hash = ht.data[hash_key];
if (node_at_hash == NULL) {
fprintf(stderr, "ERROR: key not found in hashtable");
return;
}
// if next is NULL then this is the only at this hash so just return it
if (node_at_hash->next == NULL) {
return node_at_hash->kv.value;
}
return kv->value; return kv->value;
} }
void insert_kv(HashTable *ht, char *key, char *value) { void insert_kv(HashTable *ht, char *key, char *value) {
KV *kv = find_KV_in_ht(*ht, key); int hashed_key = hash(key);
if (kv == NULL) { Node *current = ht->data[hashed_key];
int hash_val = hash(key);
if (ht->data[hash_val].key != NULL) { // value with has exists while (current) {
// come up with clever way to resolve the collision if (strcmp(current->kv.key, key) == 0) {
} else { current->kv.value = value;
ht->data[hash_val].key = key; return;
ht->data[hash_val].value = value;
} }
} else {
printf("key already in HashTable\n"); current = current->next;
} }
Node *new = new_node();
new->kv.key = key;
new->kv.value = value;
new->next = current;
ht->data[hashed_key] = new;
} }
// char *get(HashTable *h, char *key) { // char *get(HashTable *h, char *key) {
@ -102,7 +129,6 @@ int main() {
// test // test
insert_kv(h, "hello", "world"); insert_kv(h, "hello", "world");
KV *kv = find_KV_in_ht(*h, "hello");
char *result = get_value(*h, "hello"); char *result = get_value(*h, "hello");
printf("the value corresponding to the key 'hello' is: '%s'", result); printf("the value corresponding to the key 'hello' is: '%s'", result);