/*
IDE : Codeblocks 10.05
file name : hashing.cpp
*/
#include<iostream>
using namespace std;
class hashing
{
private:
int hashpos;
int array[50];
public:
hashing()
{
for(int i=0;i<50;i++)
array[i]='*';
}
int hashfunction(int );
void askdata();
int linearprobing(int);
int quadraticprobing(int);
void askdeletion();
void deletion(int);
void display();
};
void hashing::display()
{
for(int i=0;i<7;i++)
cout<<"array["<<i<<"]:"<<array[i]<<endl;
}
void hashing::askdata()
{
int data;
cout<<"Enter the data:";
cin>>data;
hashpos=hashfunction(data);
if(array[hashpos]!='*')
// hashpos=linearprobing(hashpos);
//use linear or quadratic as choice
hashpos=quadraticprobing(hashpos);
array[hashpos]=data;
cout<<endl<<"Data kept at position:"<<hashpos;
}
//........................................................
int hashing::hashfunction(int data)
{
hashpos=data%7;
return(hashpos);
}
//................................................
int hashing::linearprobing(int pos)
{
while(array[pos++]!='*')
{
if(pos==7)
pos=0;
}
return(pos);
}
//.....................................................
int hashing::quadraticprobing(int pos)
{
int i=0;
int a=pos+i;
while(array[a]!='*')
{
i++;
a=pos+i*i;
a%=7;
}
return(a);
}
//.....................................................
void hashing::askdeletion()
{
int data;
cout<<"Enter the no. to be deleted:";
cin>>data;
deletion(data);
}
void hashing::deletion(int data)
{
int flag=1,i=1;
int hashposition=hashfunction(data);
int a=hashposition;
while((a==hashfunction(array[hashposition])) || (a == (array[hashposition]=='*')))
{
if(array[hashposition]==data)
{
cout<<"Data found and deleted."<<endl;
array[hashposition]='*';
flag=0;
break;
}
if(array[hashposition]!=data)
{
hashposition=a+i*i;
hashposition%=7;
i++;
}
}
if(flag==1)
cout<<"Data not found";
}
int main()
{
hashing obj;
int choice=1;
while(choice!=4)
{
cout<<"\nEnter ur choice:\n"
<<"1.Insertion\n"
<<"2.Deletion\n"
<<"3.Display\n"
<<"4.Exit\t";
cin>>choice;
switch(choice)
{
case 1:obj.askdata();
break;
case 2:obj.askdeletion();
break;
case 3:obj.display();
}
}
return 0;
}
If any error found Drop comment's !!!
No comments:
Post a Comment