From 66f09b8a6818ce6de469aa85cb720a65133eb285 Mon Sep 17 00:00:00 2001 From: Karl Chen Date: Thu, 24 Aug 2017 21:37:09 -0700 Subject: [PATCH] Extension to 05 Update insert code Changed so that deleted slots would be replaced if insert did not replace an active key (as opposed to inserting it into the next valid slot) --- 05-methods/README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/05-methods/README.md b/05-methods/README.md index fc74c08..5cd0a15 100644 --- a/05-methods/README.md +++ b/05-methods/README.md @@ -140,6 +140,7 @@ the new item at its location. // hash_table.c void ht_insert(ht_hash_table* ht, const char* key, const char* value) { // ... + int del_index = -1; while (cur_item != NULL) {        if (cur_item != &HT_DELETED_ITEM) { if (strcmp(cur_item->key, key) == 0) { @@ -147,10 +148,19 @@ void ht_insert(ht_hash_table* ht, const char* key, const char* value) { ht->items[index] = item; return; } + } else if (del_index == -1) { + del_index = index; } - // ... + index = ht_get_hash(item->key, ht->size, i); + cur_item = ht->items[index]; + i++; } - // ... + if (del_index != -1) { + ht->items[del_index] = item; + } else { + ht->items[index] = item; + } + ht->count++; } ```