problem::
void ht_delete(ht_hash_table *ht, const char *key) {
int index = ht_get_hash(key, ht->size, 0);
ht_item *item = ht->items[index];
while (item != NULL) {
// ...
}
ht->count--;
}
In ht_delete(), ht->count -- is executed unconditionally at the end of the function.
This means that even if the target key does not exist in the hash table, the table's count will still be decreased.
expected behavior:
ht->count should only be decremented when an item is actually found and deleted.
Suggested fix
Move ht->count-- inside the successful deletion branch.
if (strcmp(item->key, key) == 0) {
ht_del_item(item);
ht->items[index] = &HT_DELETE_ITEM;
ht->count--; return;}```
problem::
In ht_delete(), ht->count -- is executed unconditionally at the end of the function.
This means that even if the target key does not exist in the hash table, the table's count will still be decreased.
expected behavior:
ht->count should only be decremented when an item is actually found and deleted.
Suggested fix
Move
ht->count--inside the successful deletion branch.