#include<iostream.h>
#include<conio.h>
#include<string.h>
class hash
{
struct node
{
char key[50];
long add;
node *next;
}*head;
public:
hash()
{
head=NULL;
}
void folding(char a[])
{
long i=0,j=0,k=0,l=0,s=0;
i=strlen(a);
j=i/2;
while(k<j)
{
l=l*10+a[k];
k++;
}
while(j<i)
{
s=s*10+a[j];
j++;
}
l=s+l;
chain(a,l);
}
void chain(char a[],long l)
{
node *temp,*r;
temp=head;
if(temp==NULL)
{
temp=new node;
strcpy(temp->key,a);
temp->add=l;
temp->next=NULL;
head=temp;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
r=new node;
strcpy( r->key,a);
r->add=l;
r->next=NULL;
temp->next=r;
}
}
void show()
{
node *temp;
temp=head;
if(head==NULL)
cout<<"\n Table is empty";
else
{
cout<<" key " <<" address ";
cout<<"\n ----........--------";
while(temp!=NULL)
{
cout<<"\n";
cout<<temp->key<<" "<<temp->add;
temp=temp->next;
}
}
}
void search(char a[])
{
long i=0,j=0,k=0,l=0,s=0;
node *temp;
temp=head;
i=strlen(a);
j=i/2;
while(k<j)
{
l=l*10+a[k];
k++;
}
while(j<i)
{
s=s*10+a[j];
j++;
}
l=s+l;
while(temp!=NULL)
{
if(temp->add==l)
{
cout<<"\n"<<a<<" is found";
s=-10;
break;
}
else
{
temp=temp->next;
}
}
if(s!=-10)
{
cout<<"\n"<<a<<" string not found ";
}
}
};
void main()
{
hash k;
clrscr();
char c='y',ch[50];
while(1)
{
cout<<"\nEnter string to save in hash table ";
cin>>ch;
k.folding(ch);
cout<<"\nFor again enter y for exit press any ";
cin>>c;
if(c!='y')
break;
}
k.show();
cout<<"\n\nFor searching Enter string ";
cin>>ch;
k.search(ch);
getch();
}
No comments:
Post a Comment