open hashing basic operations code example
Example: open hashing basic operations
//open hashing , insertion,deletion,searching,displaying
#include
using namespace std;
#define n 5
class node
{
public:
int data;
node*next;
};
node*hasharray[n];
void init()
{
for(int i=0;idata=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;idata<<" ";
temp=temp->next;
}
cout<>choice;
switch(choice)
{
case 1:
{
int val;
cout<<"enter the value you want to insert:"<>val;
addhash(val);
break;
}
case 2:
{
int val;
cout<<"enter the value you want to delete:"<>val;
int l=deletehash(val);
if(l==1)
{
cout<<"value deleted"<>val;
int l=searchhash(val);
if(l==1)
{
cout<<"value found in hash table:"<