open hashing basic operations

//open hashing , insertion,deletion,searching,displaying
#include <iostream>

using namespace std;
#define n 5
class node
{
public:
    int data;
    node*next;
};
node*hasharray[n];
void init()
{
    for(int i=0;i<n;i++)
    {
        hasharray[i]=NULL;
    }
}
void addhash(int value)
{
    node *temp=new node;
    int key=value%n;
    temp->data=value;
    temp->next=hasharray[key];
    hasharray[key]=temp;
}
int deletehash(int value)
{
    node *temp=new node;
    node*ptr=new node;
    int key=value%n;
    temp=hasharray[key];
    if(temp!=NULL)
    {
        if(temp->data==value)
        {
            ptr=temp;
            hasharray[key]=hasharray[key]->next;
            delete ptr;
            return 1;
        }
        else
        {
            while(temp->next!=NULL)
            {
                if(temp->next->data==value)
                {
                    ptr=temp->next;
                    temp->next=temp->next->next;
                    delete ptr;
                    return 1;
                }
            }
        }
    }
    return 0;
}
int searchhash(int value)
{
    int key=value%n;
    node *temp=new node;
    temp=hasharray[key];
    while(temp!=NULL)
    {
        if(temp->data==value)
        {
            return 1;
        }
        temp=temp->next;
    }
    return 0;
}
void display()
{
    for(int i=0;i<n;i++)
    {
        node *temp=new node;
        temp=hasharray[i];
        cout<<"hasharray"<<"["<<i<<"]"<<endl;
        while(temp!=NULL)
        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }
        cout<<endl;
    }
}

int main()
{
    init();
    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;
                addhash(val);
                break;
            }
        case 2:
            {
               int val;
               cout<<"enter the value you want to delete:"<<endl;
               cin>>val;
               int l=deletehash(val);
               if(l==1)
               {
                   cout<<"value deleted"<<endl;
               }
               else
               {
                   cout<<"value not found to be deleted"<<endl;
               }
               break;
            }
        case 3:
            {
              int val;
              cout<<"enter the value to be searched:"<<endl;
              cin>>val;
              int l=searchhash(val);
              if(l==1)
              {
                  cout<<"value found in hash table:"<<endl;
              }
              else
              {
                  cout<<"value not present"<<endl;
              }
              break;
            }
        case 4:
            {
                display();
                break;
            }
        case 5:
            {
                exit(0);

            }
        default:
            {
                cout<<"wrong choice :"<<endl;
                cout<<"--------------------------------------------"<<endl<<"please select between 1-5"<<endl;
                break;
            }
        }
    }

    return 0;
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source