start to implement separate chaining for resolving hash collisions
This commit is contained in:
parent
13932df1a8
commit
e819633ba0
|
@ -2,14 +2,23 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX_HASH_TABLE_SIZE 16
|
||||||
|
|
||||||
|
typedef struct KV {
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
KV data[MAX_HASH_TABLE_SIZE];
|
||||||
|
} KV;
|
||||||
|
|
||||||
typedef struct HashTable {
|
typedef struct HashTable {
|
||||||
char *data[16];
|
KV 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 < 16; i++) {
|
for (int i = 0; i < MAX_HASH_TABLE_SIZE; i++) {
|
||||||
ht->data[i] = NULL;
|
ht->data[i].key = NULL;
|
||||||
|
ht->data[i].value = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (ht);
|
return (ht);
|
||||||
|
@ -35,35 +44,67 @@ int hash(char *word) {
|
||||||
return accum % 16;
|
return accum % 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get(HashTable *h, char *key) {
|
KV *find_KV_in_ht(HashTable h, char *key) {
|
||||||
int hash_val = hash(key);
|
for (size_t i = 0; i < MAX_HASH_TABLE_SIZE; i++) {
|
||||||
return h->data[hash_val];
|
if ((h.data[i].key != NULL) && (strcmp(h.data[i].key, key) == 0)) {
|
||||||
}
|
return (&h.data[i]);
|
||||||
|
}
|
||||||
void insert(HashTable *h, char *key, char *value) {
|
|
||||||
int hashed_key = hash(key);
|
|
||||||
|
|
||||||
if (h->data[hashed_key] != NULL) {
|
|
||||||
fprintf(stderr, "ERROR: collision!");
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h->data[hashed_key] = value;
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete(HashTable *h, char *key) {
|
char *get_value(HashTable ht, char *key) {
|
||||||
int hashed_key = hash(key);
|
KV *kv = find_KV_in_ht(ht, key);
|
||||||
h->data[hashed_key] = NULL;
|
|
||||||
|
return kv->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void insert_kv(HashTable *ht, char *key, char *value) {
|
||||||
|
KV *kv = find_KV_in_ht(*ht, 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;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("key already in HashTable\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// char *get(HashTable *h, char *key) {
|
||||||
|
// int hash_val = hash(key);
|
||||||
|
// return h->data[hash_val];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void insert(HashTable *h, char *key, char *value) {
|
||||||
|
// int hashed_key = hash(key);
|
||||||
|
|
||||||
|
// if (h->data[hashed_key] != NULL) {
|
||||||
|
// fprintf(stderr, "ERROR: collision!");
|
||||||
|
// exit(1);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// h->data[hashed_key] = value;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void delete(HashTable *h, char *key) {
|
||||||
|
// int hashed_key = hash(key);
|
||||||
|
// h->data[hashed_key] = NULL;
|
||||||
|
// }
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
HashTable *hash_table = new_hashtable();
|
HashTable *h = new_hashtable();
|
||||||
char *key = "hello";
|
|
||||||
insert(hash_table, key, "world");
|
|
||||||
insert(hash_table, "hlleo", "world");
|
|
||||||
char *res = get(hash_table, "hello");
|
|
||||||
|
|
||||||
printf("the value at key '%s' is '%s'", key, res);
|
// 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);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue