Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client‘s telephone number. Make use of two collision handling techniques and compare them using number of comparisons required to find a set of telephone numbers

/**************************


Telephone Book Database using Linear Probing

 

***************************/

 

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

#define max 10

 

struct client{

    long int TeleNo;

};

class hashtable{

    client sm[max];

                public:

                                hashtable()         //constructor

                                {

                                                for(int i=0;i<max;i++)

                                                {

                                                                sm[i].TeleNo=0;

                                                               

                                                }

                                }                             

                                void insert();

                                void display();

                                void search();

                                void del();

                                int hash(long int);

};

void hashtable::insert(){

    client c;

    int Pos;

    char Ans;

    do{

        cout<< "Enter Telephone No:";

        cin>> c.TeleNo;

        Pos = hash(c.TeleNo);

        if(sm[Pos].TeleNo == 0){

            sm[Pos] = c;       

           

        }

               else

                                {

                                                while(sm[Pos].TeleNo != 0){

                                                    Pos++;

                                                }

                                                sm[Pos] =c;

                                }

                                cout<<"\n Add more data? \n Enter '1' or 'y' to continue: ";

                                cin>>Ans;

    }

    while(Ans == '1'|| Ans == 'y');

}

void hashtable::display(){

    cout<<"------------------------------------";

                cout<<"\nSrno\tPhone number\n";

                cout<<"------------------------------------\n";

                for(int i=0;i<max;i++)

                {

                                cout<<i<<"\t"<<sm[i].TeleNo<<endl;

                }

                cout<<"------------------------------------\n";

}

void hashtable::search(){

    int flag = 0;

    int x;

    cout << "Enter the Telephone No. to be searched: ";

    cin >> x;

    for(int i=0; i < max; i++){

        if(sm[i].TeleNo == x){

            cout << "\n The no. you entered is found at position: "<<i;

            flag = 1;

        }

    }

    if(flag == 0){

        cout << "\n The required no. is not present in the directory.";

    }

}

void hashtable::del(){

    int flag=0;

    int x;

    cout << "\n Enter the no. to be deleted: ";

    cin >> x;

    for(int i=0; i < max; i++){

        if(sm[i].TeleNo == x){

           

    sm[i].TeleNo = 0;

    cout << "The no. is deleted";

            flag ==1;

        break;

        }

    }

    if(flag = 0){

        cout << "The element is not found.";

    }

}

int hashtable::hash(long int key){

    return (key%max);

}

 

int main()

{

    int y,s,Choice;

                hashtable h;

                do

                {

                                cout<<"\n---------------Options---------------\n";

                                cout<<"\n1.INSERT\n2.DISPLAY\n3.SEARCH\n4.DELETE\n5.EXIT\n\n";

                                cout<<"Enter your choice:";

                                cin>>Choice;

                                cout<<"\n";

                               

                                switch(Choice)

                                {

                                                case 1:

                                                                h.insert();

                                                                cout<<"\n";

                                                                break;

                                                               

                                                case 2:

                                                                h.display();

                                                                cout<<"\n";

                                                                break;

                                                               

                                                case 3:                                                 

                                                                h.search();

                                                                cout<<"\n";

                                                                break;

                                                               

                                                case 4:

                                                                h.del();

                                                                cout<<"\n";

                                                                break;

                                                               

                                                case 5:

                                                                break;

                                }

}while(Choice!=5);                         

 

    return 0;

}

Comments

Popular posts from this blog

Find Your Purpose