Program to implement Separate Chaining in C++ STL without the use of pointers
Pre-requisite: Separate Chaining, STL in C++
This article implements the Separate Chaining in Hashing with help of STL in C++ without the use of pointers.
Approach: Make an array of vectors to get a dynamic (resizable) array for every hash index rather than using a linked list to do the same. Now it becomes easier to work on the data-set without using linked list. This simple trick is much easier to implement and is more efficient. In this approach:
- The size of the Hash is initialized by the constructor of the class.
- The elements are inserted in the Hash according to the expression:
Vector[i % n].push_back(i); where: Vector = the array of vectors used for hash i = current element to be hashed n = size of the hashAlgorithm: The algorithm for the approach is mentioned below:
- Initialize the size (say n) of vectors.
- While adding an element, go through the following steps:
- Get the value of x=”[value] MOD n“.
- Push back this element’s value in the v[x].
- For Deletion, we follow these steps:
- Get the value of x=”[value] MOD n“.
- Scan for the element to be deleted in v[x].
- If found, remove the element using erase() method.
- If element to be deleted is not found, print “Element not found!”
- Print the hash.
Implementation
// C++ Program to implement Separate// Chaining in C++ STL without// the use of pointers#include <bits/stdc++.h>usingnamespacestd;// Container for our data-setclassSeperateHash {// Data members are kept private// since they are accessed using methodsprivate:intn;vector<vector<int> > v;// Methods are kept publicpublic:// Initialising constructors as the// minimum required memory will be// allocated after which compiler// will not report flag errorSeperateHash(intn){// Constructor to initialize// the vector of vectorsv = vector<vector<int> >(n);// Here, we will allocate// memory of the unnamed_memory// to the object. This snippet// of code won't work for now.// Program will work whenever// constructor gets initializedthis->n = n;}intgetHashIndex(intx){returnx % n;}voidadd(intx){// Adding the element according// to hash indexv[getHashIndex(x)].push_back(x);}voiddel(intx){inti = getHashIndex(x);// Scan for the element to be removedfor(intj = 0; j < v[i].size(); j++) {// Erase if present otherwise// print no element found!if(v[i][j] == x) {v[i].erase(v[i].begin() + j);cout << x <<" deleted!"<< endl;return;}}cout <<"No Element Found!"<< endl;}voiddisplayHash(){// Display the contentsfor(inti = 0; i < v.size(); i++) {cout << i;for(intj = 0; j < v[i].size(); j++)cout <<" -> "<< v[i][j];cout << endl;}}};// Driver Codeintmain(){// Array to be usedintarr[] = { 12, 3, 23, 4, 11,32, 26, 33, 17, 19 };// Sending the size// for separate chainingSeperateHash obj(10);// Inserting elements in// the container accordinglyfor(inti = 0; i < 10; i++)obj.add(arr[i]);// Display the initial hashcout <<"Initial Hash:\n";obj.displayHash();// Removing the element// from the containercout <<"\nRemoving 23 from Hash: ";obj.del(23);cout << endl;// Display the final hashcout <<"Final Hash:\n";obj.displayHash();return0;}chevron_rightfilter_noneOutput:Initial Hash: 0 1 -> 11 2 -> 12 -> 32 3 -> 3 -> 23 -> 33 4 -> 4 5 6 -> 26 7 -> 17 8 9 -> 19 Removing 23 from Hash: 23 deleted! Final Hash: 0 1 -> 11 2 -> 12 -> 32 3 -> 3 -> 33 4 -> 4 5 6 -> 26 7 -> 17 8 9 -> 19
Advantages of using this approach:
- We don’t need to iterate through the entries like we need to with linked lists.
- Easier debugging of code.
- Easier to implement since the conventional approach has chances of flagging segmentation fault with minor logical errors.
- Power of applying STL methods over the data which provides an edge in competitive programming.
Recommended Posts:
- Hashing | Set 2 (Separate Chaining)
- Implementing our Own Hash Table with Separate Chaining in Java
- C++ program for hashing with chaining
- C++ Program to compare two string using pointers
- Program to implement Linear Extrapolation
- C++ Program to implement Symbol Table
- Python | Program to implement simple FLAMES game
- Method Chaining In Java with Examples
- Hashtables Chaining with Doubly Linked Lists
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- Why are elementwise additions much faster in separate loops than in a combined loop?
- Two Pointers Technique
- The length of a string using pointers
- Clone a Binary Tree with Random Pointers
- Computing index using pointers returned by STL functions in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


