Answers for "hashtable linear probing ,insertion deletion searching"

C++
0

hashtable linear probing ,insertion deletion searching

//all the basic opertaion insertion , deletion ,searching,displaying
#include <iostream>

using namespace std;
void init(int arr[],int size)
{
    for(int i=0;i<size;i++)
    {
        arr[i]=-1;
    }
}
void inserthash(int arr[],int size,int value)
{
    int key=value%size;
    int index=key;
    while(arr[index]!=-1)
    {
        index=(index+1)%size;
        if(index==key)
        {
            cout<<"hash table full"<<endl;
        }
    }
    arr[index]=value;
}
void deletehash(int arr[],int size,int value)
{
    int key=value%size;
    int index=key;
    while(arr[index]!=value)
    {
        index=(index+1)%size;
        if(index==key)
        {
            cout<<"value to be deleted not found"<<endl;
        }
    }
    arr[index]=-1;
}
int searchhash(int arr[],int size,int value)
{
    int key=value%size;
    int index=key;
    while(arr[index]!=value)
    {
        index=(index+1)%size;
        if(index==key)
        {
            return 0;
        }
    }
    return 1;

}
void display(int arr[],int size)
{
    for(int i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    int n;
    cout<<"enter the size:"<<endl;
    cin>>n;
    int a[n];
    init(a,n);
    int choice;
    while(1)
    {
        cout<<"1. insert"<<endl<<"2.delete"<<endl<<"3.search"<<endl<<"4. display"<<endl<<"5. exit"<<endl;
       cout<<"enter the choice:"<<endl;
       cin>>choice;
       switch(choice)
       {
           case 1:
               {
                   int val;
                   cout<<"enter the value you want to insert:"<<endl;
                   cin>>val;
                   inserthash(a,n,val);
                   break;
               }
           case 2:
            {
                int val;
                cout<<"enter the value you want to delete:"<<endl;
                cin>>val;
                deletehash(a,n,val);
                break;
            }
           case 3:
            {
                int val;
                cout<<"enter the value to be searched:"<<endl;
                cin>>val;
                int l= searchhash(a,n,val);
                if(l==1)
                {
                    cout<<"value found"<<endl;
                }
                else
                {
                    cout<<"not found"<<endl;
                }
                break;
            }
           case 4:
            {
                display(a,n);
                break;
            }
           case 5:
            {
                exit(0);
            }
           default:
            {
                cout<<"invalid choice"<<endl;
            }
       }
    }
}
Posted by: Guest on May-24-2021

Browse Popular Code Answers by Language