Skip to content
Related Articles

Related Articles

Improve Article
Implement Phone Directory using Hashing
  • Difficulty Level : Hard
  • Last Updated : 26 Jun, 2020

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:

  1. create_record
  2. display_record
  3. delete_record
  4. search_record
  5. update_record

Following data will be taken from the client:

ID, Name, Telephone number

Approach:
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.

  1. Create Record: This method takes details from the user like ID, Name and Telephone number and create new record in the hashtable.
  2. Display Record: This function is created to display all the record of the diary.
  3. 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.
  4. 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.
  5. 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:

C++




#include <iostream>
using namespace std;
  
// Class to store contact
// details
class node {
    string name;
    long int tel;
    int id;
  
public:
    node()
    {
        tel = 0;
        id = 0;
    }
    friend class hashing;
};
  
class hashing {
  
    // Maximum size of
    // directory is 100
    node data[100];
    string n;
    long int t;
    int i, index;
  
public:
    hashing()
    {
        i = 0;
        t = 0;
    }
  
    // This method takes details
    // from the user like ID,
    // Name and Telephone number
    // and create new record in
    // the hashtable.
    void create_record(int size)
    {
        // Enter ID
        i = 4;
  
        // Enter Name
        n = "XYZ Gupta";
  
        // Enter telephone number
        t = 23451234;
        cout << "\nEnter id :";
        cout << " \t\t\t"
             << i;
        cout << "\nEnter name :";
        cout << " \t\t\t " << n;
        cout
            << "\nEnter telephone";
        cout << " number :\t"
             << t;
        index = i % size;
  
        // Inserting record using linear
        // probing in case of collision
        for (int j = 0; j < size; j++) {
            if (data[index].id == 0) {
                data[index].id = i;
                data[index].name = n;
                data[index].tel = t;
                break;
            }
            else
                index
                    = (index + 1) % size;
        }
    }
  
    // 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.
    void search_record(int size)
    {
        int index1, key, flag = 0;
        key = 4;
        cout << "\nEnter record";
        cout << " id to search : "
             << key;
        index1 = key % size;
  
        // Traversing the directory
        // linearly inorder to search
        // record detail
        for (int a = 0; a < size; a++) {
            if (data[index1].id == key) {
                flag = 1;
                cout << "\nRecord found:";
                cout << "\n\tID ";
                cout << "\tNAME ";
                cout << "\t\tTELEPHONE ";
                cout << "\n\t"
                     << data[index1].id
                     << " \t"
                     << data[index1].name
                     << " \t"
                     << data[index1].tel;
                break;
            }
            else
                index1
                    = (index1 + 1) % size;
        }
        if (flag == 0)
            cout << "\nRecord";
        cout << " not found";
    }
  
    // 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.
    void delete_record(int size)
    {
        int index1, key, flag = 0;
        key = 4;
        cout << "\nEnter record";
        cout << " id to delete : "
             << key << "\n ";
        index1 = key % size;
        // Traversing the directory
        // linearly inorder to delete
        // the record detail
        for (int a = 0; a < size; a++) {
            if (data[index1].id
                == key) {
                flag = 1;
                data[index1].id = 0;
                data[index1].name = " ";
                data[index1].tel = 0;
                cout << "\nRecord";
                cout << " deleted";
                cout << " successfully";
                break;
            }
            else
                index1
                    = (index1 + 1) % size;
        }
        if (flag == 0)
            cout << "\nRecord";
        cout << " not found";
    }
  
    // 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.
    void update_record(int size)
    {
        int index1, key, flag = 0;
        key = 4;
        cout << "\nEnter record";
        cout << " id to update : "
             << key;
        index1 = key % size;
        // Traversing the directory
        // linearly inorder to search
        // record detail
        for (int a = 0; a < size; a++) {
            if (data[index1].id
                == key) {
                flag = 1;
                break;
            }
            else
                index1
                    = (index1 + 1) % size;
        }
        // If the record is found
        // the details are updated
        if (flag == 1) {
            n = "XYZ Agarwal";
            t = 23413421;
            data[index1].name = n;
            data[index1].tel = t;
            cout << "\nEnter";
            cout << " name: \t\t\t"
                 << n;
            cout << "\nEnter";
            cout << " telephone number: \t"
                 << t;
            cout << "\nDetails updated: ";
            cout << "\n\tID \tNAME";
            cout << " \t\tTELEPHONE ";
            cout << "\n\t"
                 << data[index1].id
                 << " \t"
                 << data[index1].name
                 << " \t"
                 << data[index1].tel;
        }
    }
  
    // This function is created to
    // display all the record of
    // the diary.
    void display_record(int size)
    {
        cout << "\n\tID \tNAME";
        cout << " \t\tTELEPHONE ";
        // Displaying the details of
        // all records of the directory.
        for (int a = 0; a < size; a++) {
            if (data[a].id != 0) {
                cout << "\n\t"
                     << data[a].id
                     << " \t"
                     << data[a].name
                     << " \t"
                     << data[a].tel;
            }
        }
    }
};
  
// Driver code
int main()
{
    // size of directory
    int size;
  
    // creating object of hashing
    // class
    hashing s;
    size = 20;
  
    // Creating a record in
    // directory
    cout << "\n1.CREATE record ";
    s.create_record(size);
  
    // Display available
    // record details
    cout << "\n\n\n\n2.DISPLAY";
    cout << " record ";
    s.display_record(size);
  
    // Searching a record detail
    // in the directory
    cout << "\n\n\n\n3.SEARCH";
    cout << " record";
    s.search_record(size);
  
    // Updating the existing
    // details of a record
    cout << "\n\n\n\n4.UPDATE";
    cout << " record ";
    s.update_record(size);
  
    // Removing specified
    // existing record
    // from dictionary
    cout << "\n\n\n\n5.DELETE";
    cout << " record ";
    s.delete_record(size);
  
    return 0;
}
Output:

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 




My Personal Notes arrow_drop_up
Recommended Articles
Page :