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.
- Menu-Driven program for Phone Directory
- MyHelper (Access your phone from anywhere without Internet)
- Double Hashing
- Hashing in Java
- Coalesced hashing
- Hashing | Set 1 (Introduction)
- Applications of Hashing
- Hashing | Set 2 (Separate Chaining)
- Hashing | Set 3 (Open Addressing)
- Quadratic Probing in Hashing
- Majority Element | Set-2 (Hashing)
- Practice Problems on Hashing
- C++ program for hashing with chaining
- Address Calculation Sort using Hashing
- Project Idea - Bio-Hashing : Two factor authentication
- Top 20 Hashing Technique based Interview Questions
- Cuckoo Hashing - Worst case O(1) Lookup!
- Extendible Hashing (Dynamic approach to DBMS)
- Rearrange characters in a string such that no two adjacent are same using hashing
- Union and Intersection of two linked lists | Set-3 (Hashing)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.