0% found this document useful (0 votes)
302 views

DSA Laboratory Practical No 1

The document describes a C++ program that implements a telephone directory using a hash table with linear probing for collision resolution. The program defines a HashFunction class with methods to insert, display, find, and delete entries from the hash table. It tests the implementation by inserting several name-number pairs, displaying the table, searching for a number, deleting an entry, and re-displaying the updated table.

Uploaded by

Ganesh Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
302 views

DSA Laboratory Practical No 1

The document describes a C++ program that implements a telephone directory using a hash table with linear probing for collision resolution. The program defines a HashFunction class with methods to insert, display, find, and delete entries from the hash table. It tests the implementation by inserting several name-number pairs, displaying the table, searching for a number, deleting an entry, and re-displaying the updated table.

Uploaded by

Ganesh Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DSA Laboratory

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:

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice: 1

Enter a Telephone No: 1234567890

Enter a Client Name: ganesh

..... Do You Want to Insert More Key Enter y/Y for yes: y

Enter a Telephone No: 1234567891

Enter a Client Name: akash

..... Do You Want to Insert More Key Enter y/Y for yes: y

Enter a Telephone No: 1234567892

Enter a Client Name: pravin

..... Do You Want to Insert More Key Enter y/Y for yes: y

Enter a Telephone No: 1234567893

Enter a Client Name: krushna

..... 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

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice: 2

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

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice: 1

Enter a Telephone No: 1234567894

Enter a Client Name: shubham

..... Do You Want to Insert More Key Enter y/Y for yes: y

Enter a Telephone No: 1234567895

Enter a Client Name: ram

..... 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

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice: 2

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

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice: 3

Enter a Key Which You Want to Search: 1234567890

1234567890 is Found at 0 Location With Name ganesh


..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice: 4

Enter a Key Which You Want to Delete: 1234567895

1234567895 is Found at 5 Location With Name ram


Key is Deleted
..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice: 2

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

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice: 5

..... Do You Want to Continue in Main Menu Enter y/Y for Yes: y

***** Dictionary (ADT) *****


1. Insert
2. Display
3. Find
4. Delete
5. Exit
..... Enter Your Choice:

You might also like