Open In App

Program to implement Separate Chaining in C++ STL without the use of pointers

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. The size of the Hash is initialized by the constructor of the class.
  2. 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 hash
    

    Separate Chaining using STL

    Algorithm: The algorithm for the approach is mentioned below:

    1. Initialize the size (say n) of vectors.
    2. While adding an element, go through the following steps:
      1. Get the value of x=”[value] MOD n“.
      2. Push back this element’s value in the v[x].
    3. For Deletion, we follow these steps:
      1. Get the value of x=”[value] MOD n“.
      2. Scan for the element to be deleted in v[x].
      3. If found, remove the element using erase() method.
      4. If element to be deleted is not found, print “Element not found!”
    4. Print the hash.

    Implementation




    // C++ Program to implement Separate
    // Chaining in C++ STL without
    // the use of pointers
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // Container for our data-set
    class SeperateHash {
      
        // Data members are kept private
        // since they are accessed using methods
    private:
        int n;
        vector<vector<int> > v;
      
        // Methods are kept public
    public:
        // Initialising constructors as the
        // minimum required memory will be
        // allocated after which compiler
        // will not report flag error
        SeperateHash(int n)
        {
            // Constructor to initialize
            // the vector of vectors
            v = 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 initialized
            this->n = n;
        }
      
        int getHashIndex(int x)
        {
            return x % n;
        }
      
        void add(int x)
        {
            // Adding the element according
            // to hash index
            v[getHashIndex(x)].push_back(x);
        }
      
        void del(int x)
        {
            int i = getHashIndex(x);
      
            // Scan for the element to be removed
            for (int j = 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;
        }
        void displayHash()
        {
            // Display the contents
            for (int i = 0; i < v.size(); i++) {
                cout << i;
                for (int j = 0; j < v[i].size(); j++)
                    cout << " -> " << v[i][j];
                cout << endl;
            }
        }
    };
      
    // Driver Code
    int main()
    {
        // Array to be used
        int arr[] = { 12, 3, 23, 4, 11,
                      32, 26, 33, 17, 19 };
      
        // Sending the size
        // for separate chaining
        SeperateHash obj(10);
      
        // Inserting elements in
        // the container accordingly
        for (int i = 0; i < 10; i++)
            obj.add(arr[i]);
      
        // Display the initial hash
        cout << "Initial Hash:\n";
        obj.displayHash();
      
        // Removing the element
        // from the container
        cout << "\nRemoving 23 from Hash: ";
        obj.del(23);
        cout << endl;
      
        // Display the final hash
        cout << "Final Hash:\n";
        obj.displayHash();
        return 0;
    }

    
    

    Output:

    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:

    1. We don’t need to iterate through the entries like we need to with linked lists.
    2. Easier debugging of code.
    3. Easier to implement since the conventional approach has chances of flagging segmentation fault with minor logical errors.
    4. Power of applying STL methods over the data which provides an edge in competitive programming.


    Last Updated : 27 Feb, 2020
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads