DSA Laboratory Practical No 1
DSA Laboratory Practical No 1
Practical no 1
Title: Consider telephone book database of N clients. Make use of a hash table implementation
to quickly look up client‘s telephone number (with Replacement)
Program:
#include<iostream>
#include<string.h>
#include<conio.h>
//using namespace std;
class HashFunction
{
typedef struct hash
{
long key;
char name[10];
}hash;
hash h[10];
public:
HashFunction();
void insert();
void display();
int find(long);
void Delete(long);
};
HashFunction::HashFunction()
{
int i;
for(i=0;i<10;i++)
{
h[i].key=-1;
strcpy(h[i].name,"NULL");
}
}
void HashFunction::Delete(long k)
{
int index=find(k);
if(index==-1)
{
std::cout<<"\n\tKey Not Found";
}
else
{
h[index].key=-1;
strcpy(h[index].name,"NULL");
std::cout<<"\n\tKey is Deleted";
}
}
int HashFunction::find(long k)
{
int i;
for(i=0;i<10;i++)
{
if(h[i].key==k)
{
std::cout<<"\n\t"<<h[i].key<<" is Found at "<<i<<" Location With Name
"<<h[i].name;
return i;
}
}
if(i==10)
{
return -1;
}
return 0;
}
void HashFunction::display()
{
int i;
std::cout<<"\n\t\tKey\t\tName";
for(i=0;i<10;i++)
{
std::cout<<"\n\th["<<i<<"]\t"<<h[i].key<<"\t\t"<<h[i].name;
}
}
void HashFunction::insert()
{
char ans,n[10],ntemp[10];
long k,temp;
int v,hi,cnt=0,flag=0,i;
do
{
if(cnt>=10)
{
std::cout<<"\n\tHash Table is FULL";
break;
}
std::cout<<"\n\tEnter a Telephone No: ";
std::cin>>k;
std::cout<<"\n\tEnter a Client Name: ";
std::cin>>n;
hi=k%10;// hash function
if(h[hi].key==-1)
{
h[hi].key=k;
strcpy(h[hi].name,n);
}
else
{
if(h[hi].key%10!=hi)
{
temp=h[hi].key;
strcpy(ntemp,h[hi].name);
h[hi].key=k;
strcpy(h[hi].name,n);
for(i=hi+1;i<10;i++)
{
if(h[i].key==-1)
{
h[i].key=temp;
strcpy(h[i].name,ntemp);
flag=1;
break;
}
}
for(i=0;i<hi && flag==0;i++)
{
if(h[i].key==-1)
{
h[i].key=temp;
strcpy(h[i].name,ntemp);
break;
}
}
}
else
{
for(i=hi+1;i<10;i++)
{
if(h[i].key==-1)
{
h[i].key=k;
strcpy(h[i].name,n);
flag=1;
break;
}
}
for(i=0;i<hi && flag==0;i++)
{
if(h[i].key==-1)
{
h[i].key=k;
strcpy(h[i].name,n);
break;
}
}
}
}
flag=0;
cnt++;
std::cout<<"\n\t..... Do You Want to Insert More Key Enter y/Y for yes: ";
std::cin>>ans;
}while(ans=='y'||ans=='Y');
int main()
{
long k;
int ch,index;
char ans;
HashFunction obj;
do
{
std::cout<<"\n\t***** Dictionary (ADT) *****";
std::cout<<"\n\t1. Insert\n\t2. Display\n\t3. Find\n\t4. Delete\n\t5. Exit";
std::cout<<"\n\t..... Enter Your Choice: ";
std::cin>>ch;
switch(ch)
{
case 1: obj.insert();
break;
case 2: obj.display();
break;
case 3: std::cout<<"\n\tEnter a Key Which You Want to Search: ";
std::cin>>k;
index=obj.find(k);
if(index==-1)
{
std::cout<<"\n\tKey Not Found";
}
break;
case 4: std::cout<<"\n\tEnter a Key Which You Want to Delete: ";
std::cin>>k;
obj.Delete(k);
break;
case 5:
break;
}
std::cout<<"\n\t..... Do You Want to Continue in Main Menu Enter y/Y for Yes: ";
std::cin>>ans;
}while(ans=='y'||ans=='Y');
}
Output:
..... Do You Want to Insert More Key Enter y/Y for yes: y
..... Do You Want to Insert More Key Enter y/Y for yes: y
..... Do You Want to Insert More Key Enter y/Y for yes: y
..... Do You Want to Insert More Key Enter y/Y for yes: n
..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y
Key Name
h[0] 1234567890 ganesh
h[1] 1234567891 akash
h[2] 1234567892 pravin
h[3] 1234567893 krushna
h[4] -1 NULL
h[5] -1 NULL
h[6] -1 NULL
h[7] -1 NULL
h[8] -1 NULL
h[9] -1 NULL
..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y
..... Do You Want to Insert More Key Enter y/Y for yes: y
..... Do You Want to Insert More Key Enter y/Y for yes: n
..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y
Key Name
h[0] 1234567890 ganesh
h[1] 1234567891 akash
h[2] 1234567892 pravin
h[3] 1234567893 krushna
h[4] 1234567894 shubham
h[5] 1234567895 ram
h[6] -1 NULL
h[7] -1 NULL
h[8] -1 NULL
h[9] -1 NULL
..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y
Key Name
h[0] 1234567890 ganesh
h[1] 1234567891 akash
h[2] 1234567892 pravin
h[3] 1234567893 krushna
h[4] 1234567894 shubham
h[5] -1 NULL
h[6] -1 NULL
h[7] -1 NULL
h[8] -1 NULL
h[9] -1 NULL
..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y
..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y