Hashing is a technique that uses fewer key comparisons and searches the element in O(n) time in the worst case and in O(1) time in the average case.
- The task is to implement all functions of phone directory:
Following data will be taken from the client:
ID, Name, Telephone number
We are creating a hash table, and inserting records. For deleting, searching, or updating an entity, the client ID is asked and on the basis of equality operator, details are displayed or processed. If the record is not found, then an appropriate message is displayed.
Collision is the major problem in the hashing technique. In open addressing (closed hashing), all collisions are resolved in the prime area i.e., the area that contains all of the home addresses.
When a collision occurs, the prime area addresses are searched for an open or unoccupied element using linear probing.
Steps for inserting entities in a hash table:
1. If the location is empty, directly insert the entity.
2. If mapped location is occupied then keep probing until an empty slot is found. Once an empty slot is found, insert the entity.
- Create Record: This method takes details from the user like ID, Name and Telephone number and create new record in the hashtable.
- Display Record: This function is created to display all the record of the diary.
- Delete Record: This method takes the key of the record to be deleted. Then, it searches in hash table if record id matches with the key. Then, that record is deleted.
- Search Record: This method takes the key of the record to be searched. Then, it traverses the hash table, if record id matches with the key it displays the record detail.
- Update Record: This method takes the key of the record to be searched. Then, it traverses the hash table, if record id matches with the key then it displays the record detail.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes