Add/Remove & Search in Hash Table (Implementation)
This lesson will cover the C++ Implementation for search, insertion, and deletion in hash tables.
We'll cover the following
In the previous lesson, we built a HashTable
class and designed a hash function. Using that code, we will implement the main functionalities of a hash table.
Insertion in Table #
Insertion in hash tables is a simple task, and it usually takes a constant amount of time. When the hash function returns the index for our input key, we check if there is a hash entry already present at that index (if it does, a collision has occurred). If not, we simply create a new hash entry for the key/value. However, if the index is not NULL
, we will traverse through the bucket to check if an object with our key exists. A display
function is used to display all the key/value pairs of a hash table.
It is possible that the key we are inserting already exists. In this case, we will simply update the value. Otherwise, we add the new entry at the next available space of the bucket.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.