#include<iostream>
#include<cstdlib>
using namespace std;
class ihash
{
int *p;
int d;
public:
ihash()
{
d=10;
}
void insert(int);
bool search(int);
void del(int);
void display();
void inp();
};
void ihash::inp()
{
p=new int[10];
for(int i=0;i<10;i++)
p[i]=-1;
}
void ihash::insert(int key)
{
int i;
i=key%d;
if(p[i]==-1)
p[i]=key;
else
{
int j=i;
i=(i+1)%d;
while(i!=j)
{
if(p[i]==-1)
{
p[i]=key;
break;
}
else
i=(i+1)%d;
}
}
}
bool ihash::search(int key)
{
int i,j;
i=key%d;
if(p[i]==key)
return true;
else
{
j=i;
i=(i+1)%d;
while(i!=j)
{
if(p[i]==key)
return key;
else
i=(i+1)%d;
}
return false;
}
}
void ihash::del(int key)
{
int i,j;
i=key%d;
if(p[i]==key)
p[i]=-1;
else
{
j=i;
i=(i+1)%d;
while(i!=j)
{
if(p[i]==key)
p[i]=-1;
else
i=(i+1)%d;
}
}
}
void ihash::display()
{
for(int i=0;i<10;i++)
{
if(p[i]==-1)
cout<<"NULL\n";
else
cout<<p[i]<<"\n";
}
}
int main()
{
int option;
bool ans;
int key;
ihash ih;
ih.inp();
while(1)
{
cout<<"\nEnter your choice\n";
cout<<"\n1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n";
cin>>option;
switch(option)
{
case 1:cout<<"\nEnter the value your want to insert\n";
cin>>key;
ih.insert(key);
break;
case 2:cout<<"\nEnter the value you want to delete\n";
cin>>key;
ih.del(key);
break;
case 3:cout<<"\nEnter the value you want to search\n";
cin>>key;
ans=ih.search(key);
if(ans)
cout<<"\nFound\n";
else
cout<<"\nMissing\n";
break;
case 4:ih.display();
break;
case 5:exit(1);
}
}
return 0;
}
OUTPUT:-
Enter your choice
1.Insert
2.Delete
3.Search
4.Display
5.Exit
1
Enter the value your want to insert
2
Enter your choice
1.Insert
2.Delete
3.Search
4.Display
5.Exit
1
Enter the value your want to insert
4
Enter your choice
1.Insert
2.Delete
3.Search
4.Display
5.Exit
1
Enter the value your want to insert
6
Enter your choice
1.Insert
2.Delete
3.Search
4.Display
5.Exit
4
NULL
NULL
2
NULL
4
NULL
6
NULL
NULL
NULL
Enter your choice
1.Insert
2.Delete
3.Search
4.Display
5.Exit
3
Enter the value you want to search
4
Found
Enter your choice
1.Insert
2.Delete
3.Search
4.Display
5.Exit
2
Enter the value you want to delete
6
Enter your choice
1.Insert
2.Delete
3.Search
4.Display
5.Exit
4
NULL
NULL
2
NULL
4
NULL
NULL
NULL
NULL
NULL
Enter your choice
1.Insert
2.Delete
3.Search
4.Display
5.Exit
5
No comments:
Post a Comment