what do you guys think of this add function in the HashTable I created?
Code:
boolean HashTableInsert(HashTable *this, const char* key, const char* value)
{
list_t *new_list;
list_t *current_list;
int_u4 hashval = StringHash(key) % this->length;
/* Attempt to allocate memory for list */
if ((new_list = malloc(sizeof(list_t))) == NULL)
outOfMemory(__LINE__, __FILE__, "struct node creation");
/* Does item already exist? */
if (HashTableContains(this, key)){
char* val = HashTableLookUp(this, key);
StringDestruct(val);
val = StringConstruct(value);
return false;
}
else{
/* Insert into list */
new_list->key = StringConstruct(key);
new_list->value = StringConstruct(value);
new_list->next = this->table[hashval];
this->table[hashval] = new_list;
return true;
}
}
it basically just frees the old value and copies the new value in if the key already exists.. this is the part where I was sure to get.. otherwise it just adds in the key value to the hash table
HashTableLookUp funtion here just looks up the given key and return a "reference" to the given value.