In hashing there is a hash function that maps keys to some values. But these hashing function may lead to collision that is two or more keys are mapped to same value. Chain hashing avoids collision. The idea is to make each cell of hash table point to a linked list of records that have same hash function value.
Let’s create a hash function, such that our hash table has ‘N’ number of buckets.
To insert a node into the hash table, we need to find the hash index for the given key. And it could be calculated using the hash function.
Example: hashIndex = key % noOfBuckets
Insert: Move to the bucket corresponds to the above calculated hash index and insert the new node at the end of the list.
Delete: To delete a node from hash table, calculate the hash index for the key, move to the bucket corresponds to the calculated hash index, search the list in the current bucket to find and remove the node with the given key (if found).
Please refer Hashing | Set 2 (Separate Chaining) for details.
We use a list in C++ which is internally implemented as linked list (Faster insertion and deletion).
0 1 --> 15 --> 8 2 3 4 --> 11 5 6 --> 27
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.
- Hashing | Set 2 (Separate Chaining)
- Program to implement Separate Chaining in C++ STL without the use of pointers
- Implementing our Own Hash Table with Separate Chaining in Java
- Hashtables Chaining with Doubly Linked Lists
- Convert an array to reduced form | Set 1 (Simple and Hashing)
- Cuckoo Hashing - Worst case O(1) Lookup!
- Top 20 Hashing Technique based Interview Questions
- Hashing | Set 1 (Introduction)
- Union and Intersection of two linked lists | Set-3 (Hashing)
- Index Mapping (or Trivial Hashing) with negatives allowed
- Practice Problems on Hashing
- Double Hashing
- Coalesced hashing
- Majority Element | Set-2 (Hashing)
- Applications of Hashing
- Hashing in Java
- Address Calculation Sort using Hashing
- Rearrange characters in a string such that no two adjacent are same using hashing
- Extendible Hashing (Dynamic approach to DBMS)
- Area of the largest square that can be formed from the given length sticks using 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.