Also in line 12 it checks to see if the key doesn't match either so wouldn't that work? |
No. Although you check for the end of the list at line 16, that doesn't help if there is exactly one item in the list. When there is one item, head != nullptr so it goes to line 12. Then cur->next == nullptr so it skips the while loop. Then it deletes the item in the list.
It would if your
remove()
function worked.
Regarding your later post of your full code:
Line 32:
hashValue = rand() % size + 0;
. You have a fundamental misunderstanding of what a hash function is. The idea of a hash function is that
hashing the same key always generates the same hash value. Your hash function is generating a random number between 0 and size. The reason you want to generate the same hash value is that it leads you to a specific bucket where the item must be located.
Line 55:
List->clear();
This clears one list. You need to clear them all. Just call clear() instead since that method clears all lists.
HashTable::find(). You should hash the key to lead you straight to the bucket where the item must be, if it's in the hash table. Again, this is the purpose for using a hash table: the hash function quickly leads you to the right bucket.
HashTable::isEmpty() and HashTable::isFull(): you're checking only the first bucket.
DynamicList::insert(): Are you sure that items should be inserted in sorted order? If the list should be sorted then you should remove the append() method. Usually singly linked lists are used when the order doesn't matter and you always insert at the head of the list because it's very fast.
DynamicList::isFull(): what determines if a list is full? You're testing whether the program runs out of memory.