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 {
char *key;
char *value;
KV data[MAX_HASH_TABLE_SIZE];
} KV;
typedef struct Node {
KV kv;
struct Node *next;
} Node;
typedef struct HashTable {
KV data[MAX_HASH_TABLE_SIZE];
Node *data[MAX_HASH_TABLE_SIZE];
} HashTable;
HashTable *new_hashtable() {
HashTable *ht = malloc(sizeof(HashTable));
for (int i = 0; i < MAX_HASH_TABLE_SIZE; i++) {
ht->data[i].key = NULL;
ht->data[i].value = NULL;
ht->data[i] = NULL;
}
return (ht);
}
Node *new_node() {
Node *node = malloc(sizeof(Node));
node->next = NULL;
return (node);
}
int get_index_for_letter(char letter) {
if (letter >= 'a' && letter <= 'z') {
return (letter - 'a') + 1;
@ -46,8 +56,8 @@ int hash(char *word) {
KV *find_KV_in_ht(HashTable h, char *key) {
for (size_t i = 0; i < MAX_HASH_TABLE_SIZE; i++) {
if ((h.data[i].key != NULL) && (strcmp(h.data[i].key, key) == 0)) {
return (&h.data[i]);
if ((h.data[i] != NULL) && (strcmp(h.data[i]->kv.key, key) == 0)) {
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) {
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;
}
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) {
int hash_val = hash(key);
if (ht->data[hash_val].key != NULL) { // value with has exists
// come up with clever way to resolve the collision
} else {
ht->data[hash_val].key = key;
ht->data[hash_val].value = value;
Node *current = ht->data[hashed_key];
while (current) {
if (strcmp(current->kv.key, key) == 0) {
current->kv.value = value;
return;
}
} 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) {
@ -102,7 +129,6 @@ int main() {
// test
insert_kv(h, "hello", "world");
KV *kv = find_KV_in_ht(*h, "hello");
char *result = get_value(*h, "hello");
printf("the value corresponding to the key 'hello' is: '%s'", result);