File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
#include<iostream>
#include<stdlib.h>
#include<string.h>
#define MAX 10
using namespace std;
int count=0;
struct ht
{
long int phone;
char name[MAX];
} rec[MAX],temp,tmp;
class hashtable
{
public:
hashtable();
int hashing(int);
int insert(struct ht);
void display();
void find();
};
hashtable::hashtable()
{
int i;
for(i=0;i<MAX;i++)
{
rec[i].phone=-1;
strcpy(rec[i].name,"-");
}
}
int hashtable::hashing(int no)
{
int hv;
hv=no%MAX;
return hv;
}
int hashtable::insert(struct ht h)
{
int i,k,p;
k=hashing(h.phone);
if(rec[k].phone==-1)
{
rec[k].phone=h.phone;
strcpy(rec[k].name,h.name);
count++;
}
else
{
for(i=(k+1)%MAX;i<MAX;i=(i+1)%MAX)
{
if(rec[i].phone==-1)
{
rec[i].phone=h.phone;
strcpy(rec[i].name,h.name);
count++;
break;
}
}
}
return count;
}
Page 1 of 5File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
void hashtable::display()
{
if(count!=0)
{
cout<<"\n
HASH TABLE";
cout<<"\nIndex\tName\tPhone\n";
for(int i=0;i<MAX;i++)
{
cout<<i;
cout<<"\t"<<rec[i].name;
cout<<"\t"<<rec[i].phone;
cout<<"\n";
}
}
else
cout<<"No entries in the table(empty)";
}
void hashtable::find()
{
long int phone;
int k,i,p,flag=0;
cout<<"\nEnter the phone number to search: ";
cin>>phone;
k=hashing(phone);
for(i=k;i<MAX;i=(i+1)%MAX)
{
if(count==MAX)
break;
else
{
if(rec[i].phone==phone)
{
cout<<"Details Found: ";
cout<<"\t"<<i;
cout<<"\t"<<rec[i].phone;
cout<<"\t"<<rec[i].name;
cout<<"\n";
flag=1;
count++;
break;
}
else
count++;
}
}
if(flag==0)
cout<<"Details not found.";
}
int main()
{
hashtable obj;
int ch;
char ans;
struct ht temph;
do
{
cout<<"\n\tHASH TABLE";
cout<<"\n1.Insert data";
cout<<"\n2.Display data";
cout<<"\n3.Find data";
cout<<"\n4.Exit";
cout<<"\nEnter your choice:\t";
cin>>ch;
switch(ch)
Page 2 of 5File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
{
case 1: if(count!=MAX)
{
cout<<"\nEnter name:\t";
cin>>temph.name;
cout<<"Enter phone:\t";
cin>>temph.phone;
count=obj.insert(temph);
}
else
cout<<"\nHash table is full.";
break;
case 2: obj.display();
break;
case 3: obj.find();
break;
case 4: exit(0);
}
cout<<"\nDo you want to continue? ";
cin>>ans;
} while(ans=='y'||ans=='Y');
return 0;
}
/*
OUTPUT
saiesh@saiesh :~$ g++ ads5.cpp
saiesh@saiesh :~$ ./a.out
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Saiesh
Enter phone:
9811
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Rohan
Enter phone:
9812
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Payal
Enter phone:
9813
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
Page 3 of 5File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
4.Exit
Enter your choice:
1
Enter name:
Pratik
Enter phone:
9829
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Karishma
Enter phone:
9822
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Misba
Enter phone:
9819
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
2
HASH TABLE
Index
Name
Phone
0
Misba
9819
1
Saiesh 9811
2
Rohan
9812
3
Payal
9813
4
Karishma
9822
5
-
-1
6
-
-1
7
-
-1
8
-
-1
9
Pratik 9829
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter the phone number to search: 9811
Details Found: 1
9811
Saiesh
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Page 4 of 5File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
Enter your choice:
3
Enter the phone number to search: 9829
Details Found: 9
9829
Pratik
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter the phone number to search: 9819
Details Found: 0
9819
Misba
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter the phone number to search: 9826
Details not found.
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
saiesh@saiesh :~$ */
4
Page 5 of 5
#include<iostream>
#include<stdlib.h>
#include<string.h>
#define MAX 10
using namespace std;
int count=0;
struct ht
{
long int phone;
char name[MAX];
} rec[MAX],temp,tmp;
class hashtable
{
public:
hashtable();
int hashing(int);
int insert(struct ht);
void display();
void find();
};
hashtable::hashtable()
{
int i;
for(i=0;i<MAX;i++)
{
rec[i].phone=-1;
strcpy(rec[i].name,"-");
}
}
int hashtable::hashing(int no)
{
int hv;
hv=no%MAX;
return hv;
}
int hashtable::insert(struct ht h)
{
int i,k,p;
k=hashing(h.phone);
if(rec[k].phone==-1)
{
rec[k].phone=h.phone;
strcpy(rec[k].name,h.name);
count++;
}
else
{
for(i=(k+1)%MAX;i<MAX;i=(i+1)%MAX)
{
if(rec[i].phone==-1)
{
rec[i].phone=h.phone;
strcpy(rec[i].name,h.name);
count++;
break;
}
}
}
return count;
}
Page 1 of 5File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
void hashtable::display()
{
if(count!=0)
{
cout<<"\n
HASH TABLE";
cout<<"\nIndex\tName\tPhone\n";
for(int i=0;i<MAX;i++)
{
cout<<i;
cout<<"\t"<<rec[i].name;
cout<<"\t"<<rec[i].phone;
cout<<"\n";
}
}
else
cout<<"No entries in the table(empty)";
}
void hashtable::find()
{
long int phone;
int k,i,p,flag=0;
cout<<"\nEnter the phone number to search: ";
cin>>phone;
k=hashing(phone);
for(i=k;i<MAX;i=(i+1)%MAX)
{
if(count==MAX)
break;
else
{
if(rec[i].phone==phone)
{
cout<<"Details Found: ";
cout<<"\t"<<i;
cout<<"\t"<<rec[i].phone;
cout<<"\t"<<rec[i].name;
cout<<"\n";
flag=1;
count++;
break;
}
else
count++;
}
}
if(flag==0)
cout<<"Details not found.";
}
int main()
{
hashtable obj;
int ch;
char ans;
struct ht temph;
do
{
cout<<"\n\tHASH TABLE";
cout<<"\n1.Insert data";
cout<<"\n2.Display data";
cout<<"\n3.Find data";
cout<<"\n4.Exit";
cout<<"\nEnter your choice:\t";
cin>>ch;
switch(ch)
Page 2 of 5File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
{
case 1: if(count!=MAX)
{
cout<<"\nEnter name:\t";
cin>>temph.name;
cout<<"Enter phone:\t";
cin>>temph.phone;
count=obj.insert(temph);
}
else
cout<<"\nHash table is full.";
break;
case 2: obj.display();
break;
case 3: obj.find();
break;
case 4: exit(0);
}
cout<<"\nDo you want to continue? ";
cin>>ans;
} while(ans=='y'||ans=='Y');
return 0;
}
/*
OUTPUT
saiesh@saiesh :~$ g++ ads5.cpp
saiesh@saiesh :~$ ./a.out
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Saiesh
Enter phone:
9811
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Rohan
Enter phone:
9812
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Payal
Enter phone:
9813
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
Page 3 of 5File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
4.Exit
Enter your choice:
1
Enter name:
Pratik
Enter phone:
9829
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Karishma
Enter phone:
9822
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter name:
Misba
Enter phone:
9819
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
2
HASH TABLE
Index
Name
Phone
0
Misba
9819
1
Saiesh 9811
2
Rohan
9812
3
Payal
9813
4
Karishma
9822
5
-
-1
6
-
-1
7
-
-1
8
-
-1
9
Pratik 9829
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter the phone number to search: 9811
Details Found: 1
9811
Saiesh
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Page 4 of 5File: /home/saiesh/ADS/Assignment No 5/ads5.cpp
Enter your choice:
3
Enter the phone number to search: 9829
Details Found: 9
9829
Pratik
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter the phone number to search: 9819
Details Found: 0
9819
Misba
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter the phone number to search: 9826
Details not found.
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
saiesh@saiesh :~$ */
4
Page 5 of 5
Comments
Post a Comment