Implement direct access file using hashing (chaining without replacement) and
perform following operations on it.
*Store data of students with telephone no and name in the structure using hashing function for telephone number and implement chaining with and
without replacement.*/
without replacement.*/
#include <iostream>
#include <string.h>
#define MAX 10
using namespace std;
class hashing //ADT
{
struct student
{
long telephone_no; //telephone number
char name[MAX]; //name of the student
int chain; //chain for chaining
};
struct student h[MAX];
public:
hashing(); //default constructor
void Insert_wor(); //chaining without replacement
void display(); //display
void Insert_wr(); //chaining with replacement
int hash(long); //hash function
};
#include <string.h>
#define MAX 10
using namespace std;
class hashing //ADT
{
struct student
{
long telephone_no; //telephone number
char name[MAX]; //name of the student
int chain; //chain for chaining
};
struct student h[MAX];
public:
hashing(); //default constructor
void Insert_wor(); //chaining without replacement
void display(); //display
void Insert_wr(); //chaining with replacement
int hash(long); //hash function
};
hashing::hashing()
{
int i;
for(i=0;i<MAX;i++)
{
h[i].telephone_no=-1;
h[i].name[0]='\0';
h[i].chain=-1;
}
}
{
int i;
for(i=0;i<MAX;i++)
{
h[i].telephone_no=-1;
h[i].name[0]='\0';
h[i].chain=-1;
}
}
int hashing::hash(long tno)
{
return(tno%MAX);
}
void hashing::Insert_wor()
{
int index,index1,i;
long tno;
char nm[MAX],ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter Telephone number and Name of the student: ";
cin>>tno>>nm;
cout<<nm;
index=hash(tno);
if(h[index].telephone_no==-1) //if empty slot
{
//store the record
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
}
else
{
while(h[index].chain!=-1)
index=h[index].chain;
index1=index;
//apply linear probing technique
do
{
index=(index+1)%MAX;
}while((index!=index1) && (h[index].telephone_no!=-1));
if(index==index1)
cout<<"\nOverflow";
else
{
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
h[index1].chain=index;
}
}
display();
cout<<"\n Do you wanna enter 1 more record? (y/n)?";
cin>>ch;
}
}
{
return(tno%MAX);
}
void hashing::Insert_wor()
{
int index,index1,i;
long tno;
char nm[MAX],ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter Telephone number and Name of the student: ";
cin>>tno>>nm;
cout<<nm;
index=hash(tno);
if(h[index].telephone_no==-1) //if empty slot
{
//store the record
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
}
else
{
while(h[index].chain!=-1)
index=h[index].chain;
index1=index;
//apply linear probing technique
do
{
index=(index+1)%MAX;
}while((index!=index1) && (h[index].telephone_no!=-1));
if(index==index1)
cout<<"\nOverflow";
else
{
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
h[index1].chain=index;
}
}
display();
cout<<"\n Do you wanna enter 1 more record? (y/n)?";
cin>>ch;
}
}
void hashing::Insert_wr()
{
int index,index1,index2,i,f=0;
long tno;
student s;
char nm[MAX],nm1[MAX],ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter Telephone number and Name of the student: ";
cin>>tno>>nm;
cout<<nm;
index=hash(tno);
if(h[index].telephone_no==-1) //if empty slot
{
//store the record
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
}
else
{
//1. for synonym
if(hash(h[index].telephone_no)==index)
{
//traverse chain
while(h[index].chain!=-1)
index=h[index].chain;
// Linear Probing
index1=index;
do
{
index=(index+1)%MAX;
}while((index!=index1) && (h[index].telephone_no!=-1));
if(index==index1)
cout<<"\nOverflow";
else
{
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
h[index1].chain=index;
}
}
//Not a synonym
else if(hash(h[index].telephone_no)!=index)
{
s.telephone_no=h[index].telephone_no;
strcpy(s.name,h[index].name);
s.chain=h[index].chain;
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
h[index].chain=-1;
// Linear Probing
index1=index;
do
{
index=(index+1)%MAX;
}while((index!=index1) && (h[index].telephone_no!=-1));
if(index==index1)
cout<<"\nOverflow";
else
{
h[index].telephone_no=s.telephone_no;
strcpy(h[index].name,s.name);
h[index].chain=s.chain;
index2=0;
while(h[index2].chain!=index1)
index2++;
h[index2].chain=index;
}
}
}
display();
cout<<"\n Do you wanna enter 1 more record? (y/n)?";
cin>>ch;
}
}
{
int index,index1,index2,i,f=0;
long tno;
student s;
char nm[MAX],nm1[MAX],ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter Telephone number and Name of the student: ";
cin>>tno>>nm;
cout<<nm;
index=hash(tno);
if(h[index].telephone_no==-1) //if empty slot
{
//store the record
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
}
else
{
//1. for synonym
if(hash(h[index].telephone_no)==index)
{
//traverse chain
while(h[index].chain!=-1)
index=h[index].chain;
// Linear Probing
index1=index;
do
{
index=(index+1)%MAX;
}while((index!=index1) && (h[index].telephone_no!=-1));
if(index==index1)
cout<<"\nOverflow";
else
{
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
h[index1].chain=index;
}
}
//Not a synonym
else if(hash(h[index].telephone_no)!=index)
{
s.telephone_no=h[index].telephone_no;
strcpy(s.name,h[index].name);
s.chain=h[index].chain;
h[index].telephone_no=tno;
strcpy(h[index].name,nm);
h[index].chain=-1;
// Linear Probing
index1=index;
do
{
index=(index+1)%MAX;
}while((index!=index1) && (h[index].telephone_no!=-1));
if(index==index1)
cout<<"\nOverflow";
else
{
h[index].telephone_no=s.telephone_no;
strcpy(h[index].name,s.name);
h[index].chain=s.chain;
index2=0;
while(h[index2].chain!=index1)
index2++;
h[index2].chain=index;
}
}
}
display();
cout<<"\n Do you wanna enter 1 more record? (y/n)?";
cin>>ch;
}
}
void hashing::display()
{
cout<<"\nIndex\tTelephone no\tName\tChain\n------------------------------------------------------------------------\n";
for(int i=0;i<MAX;i++)
cout<<i<<"\t"<<h[i].telephone_no<<"\t"<<h[i].name<<"\t"<<h[i].chain<<endl;
}
{
cout<<"\nIndex\tTelephone no\tName\tChain\n------------------------------------------------------------------------\n";
for(int i=0;i<MAX;i++)
cout<<i<<"\t"<<h[i].telephone_no<<"\t"<<h[i].name<<"\t"<<h[i].chain<<endl;
}
int main()
{
hashing obj;
int ch=1;
while(ch)
{
cout<<"\nMenu\n1.Chaining without replacement\n2.Chaining with replacement\n3.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
obj.Insert_wor();
break;
case 2:
obj.Insert_wr();
break;
case 3:
break;
default:
cout<<"\nWrong choice!!!";
}
}
return 0;
}
{
hashing obj;
int ch=1;
while(ch)
{
cout<<"\nMenu\n1.Chaining without replacement\n2.Chaining with replacement\n3.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
obj.Insert_wor();
break;
case 2:
obj.Insert_wr();
break;
case 3:
break;
default:
cout<<"\nWrong choice!!!";
}
}
return 0;
}
No comments:
Post a Comment