Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client's telephone number
#include<iostream>
#include<string.h>
using namespace std;
struct data
{ char name[30];
long teleno;
};
class hash
{ int n,sum,x,c,i,j; char na[30]; long no;
data d[10];
public:
hash()
{ for(i=0;i<10;i++)
{ d[i].teleno=0; }
}
void insert();
void search();
void display();
};
void hash::insert()
{
cout<<"\n enter no. of clients";
cin>>n; //d[x].name
for(j=0;j<n;j++)
{ cout<<"\n enter name of client";
cin>>na;
cout<<"\n enter telephone no. of client";
cin>>no;
sum=0;
for(i=0;i<strlen(na);i++)
{ sum=sum+na[i];
}
x=(sum/strlen(na))%10;
cout<<x;
c=x;
while(1)
{
if(d[x].teleno==0)
{ strcpy(d[x].name,na);
d[x].teleno=no;
break;
}
x=(x+1)%10;
if(c==x)
{ cout<<"\n hash table is full";
break;
}
}
}
}
void hash::search()
{ cout<<"\n enter name to be searched";
cin>>na;
sum=0;
for(i=0;i<strlen(na);i++)
{ sum=sum+(int)na[i];
}
x=(sum/strlen(na))%10;
c=x;
while(1)
{
if(!strcmp(d[x].name,na))
{ cout<<"\n data found : TELEPHONE NO:"<<d[x].teleno;
break;
}
x=(x+1)%10;
if(c==x)
{ cout<<"\n data not found";
break;
}
}
}
void hash::display()
{
for(int i=0;i<10;i++){
cout<<endl<<d[i].name<<" "<<d[i].teleno;
}
}
int main()
{
hash h;
h.insert();
// h.display();
h.search();
// h.display();
return 0;
}
#include<string.h>
using namespace std;
struct data
{ char name[30];
long teleno;
};
class hash
{ int n,sum,x,c,i,j; char na[30]; long no;
data d[10];
public:
hash()
{ for(i=0;i<10;i++)
{ d[i].teleno=0; }
}
void insert();
void search();
void display();
};
void hash::insert()
{
cout<<"\n enter no. of clients";
cin>>n; //d[x].name
for(j=0;j<n;j++)
{ cout<<"\n enter name of client";
cin>>na;
cout<<"\n enter telephone no. of client";
cin>>no;
sum=0;
for(i=0;i<strlen(na);i++)
{ sum=sum+na[i];
}
x=(sum/strlen(na))%10;
cout<<x;
c=x;
while(1)
{
if(d[x].teleno==0)
{ strcpy(d[x].name,na);
d[x].teleno=no;
break;
}
x=(x+1)%10;
if(c==x)
{ cout<<"\n hash table is full";
break;
}
}
}
}
void hash::search()
{ cout<<"\n enter name to be searched";
cin>>na;
sum=0;
for(i=0;i<strlen(na);i++)
{ sum=sum+(int)na[i];
}
x=(sum/strlen(na))%10;
c=x;
while(1)
{
if(!strcmp(d[x].name,na))
{ cout<<"\n data found : TELEPHONE NO:"<<d[x].teleno;
break;
}
x=(x+1)%10;
if(c==x)
{ cout<<"\n data not found";
break;
}
}
}
void hash::display()
{
for(int i=0;i<10;i++){
cout<<endl<<d[i].name<<" "<<d[i].teleno;
}
}
int main()
{
hash h;
h.insert();
// h.display();
h.search();
// h.display();
return 0;
}
Bahut error he bhaii
ReplyDelete# Consider telephone book database of N clients. Make use of a hash table implementation
Delete# to quickly look up client‘s telephone number. Make use of two collision handling
# techniques and compare them using number of comparisons required to find a set of
# telephone numbers
def tele_database():
phone_data = []
n = int(input("Enter Number of Clients :- "))
print("Enter Phone Numbers --\n")
for _ in range(n):
x = int(input("--> "))
phone_data.append(x)
return phone_data
def hash_function_1(key_ele, m_size):
h1 = key_ele % m_size
return h1
def hash_function_2(key_ele):
h2 = 1 + (key_ele % 11)
return h2
def hashtable(ht):
print(f"\nHash Value \tKey")
for ele in range(len(ht)):
if ht[ele] != -1:
print(f"\n\t{ele} \t---> \t{ht[ele]}")
else:
print(f"\n\t{ele}")
phone_database = tele_database()
m = int(input("Enter Hash Table Size :- "))
hash_table = [-1] * m
opt = int(input("If collision occurs which collision resolution technique do you want to use?\n\t1. Linear "
"Probing\n\t2. Double Hashing :- "))
for k in phone_database:
h_1 = hash_function_1(k, m)
h_2 = hash_function_2(k)
if hash_table[h_1] == -1:
hash_table[h_1] = k
else:
if opt == 1:
while hash_table[h_1] != -1:
h_1 += 1
hash_table[h_1] = k
elif opt == 2:
i = 0
while hash_table[h_1] != -1:
i += 1
h_1 = (h_1 + (i * h_2)) % m
hash_table[h_1] = k
hashtable(hash_table)
Galat yeto de dadya
ReplyDelete