File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
#include<iostream>
#include<stdlib.h>
#include<string.h>
#define MAX 10
using namespace std;
int count=0;
struct ht
{
int id;
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].id=-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;
k=hashing(h.id);
if(rec[k].id==-1)
{
rec[k].id=h.id;
strcpy(rec[k].name,h.name);
count++;
}
else
{
for(i=(k+1)%MAX;i<MAX;i=(i+1)%MAX)
{
if(rec[i].id==-1)
{
rec[i].id=h.id;
strcpy(rec[i].name,h.name);
count++;
break;
}
}
}
return count;
}
Page 1 of 5File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
void hashtable::display()
{
if(count!=0)
{
cout<<"\n
HASH TABLE";
cout<<"\nIndex\tName\tID\n";
for(int i=0;i<MAX;i++)
{
cout<<i;
cout<<"\t"<<rec[i].name;
cout<<"\t"<<rec[i].id;
cout<<"\n";
}
}
else
cout<<"No entries in the table(empty)";
}
void hashtable::find()
{
int id,k,i,p,flag=0,count=0;
cout<<"\nEnter ID to search: ";
cin>>id;
k=hashing(id);
for(i=k;i<MAX;i=(i+1)%MAX)
{
if(count==MAX)
break;
else
{
if(rec[i].id==id)
{
cout<<"ID is present at location: "<<i;
flag=1;
count++;
break;
}
else
count++;
}
}
if(flag==0)
cout<<"ID not found.";
}
int main()
{
hashtable obj;
int ch,ch1,no,size;
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)
{
case 1: if(count!=MAX)
{
cout<<"\nEnter ID:\t";
cin>>temph.id;
Page 2 of 5File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
cout<<"Enter name:\t";
cin>>temph.name;
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++ ads6.cpp
saiesh@saiesh :~$ ./a.out
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
11
Enter name:
Saiesh
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
12
Enter name:
Rohan
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
13
Enter name:
Payal
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
Enter ID:
Enter name:
29
Pratik
1
Page 3 of 5File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
22
Enter name:
Karishma
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
19
Enter name:
Misba
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
ID
Name
0
19
Misba
1
11
Saiesh
2
12
Rohan
3
13
Payal
4
22
Karishma
5
-1
-
6
-1
-
7
-1
-
8
-1
-
9
29
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 ID to search: 11
ID is present at location: 1
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter ID to search: 29
ID is present at location: 9
Do you want to continue? y
Page 4 of 5File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter ID to search: 19
ID is present at location: 0
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter ID to search: 26
ID 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
{
int id;
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].id=-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;
k=hashing(h.id);
if(rec[k].id==-1)
{
rec[k].id=h.id;
strcpy(rec[k].name,h.name);
count++;
}
else
{
for(i=(k+1)%MAX;i<MAX;i=(i+1)%MAX)
{
if(rec[i].id==-1)
{
rec[i].id=h.id;
strcpy(rec[i].name,h.name);
count++;
break;
}
}
}
return count;
}
Page 1 of 5File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
void hashtable::display()
{
if(count!=0)
{
cout<<"\n
HASH TABLE";
cout<<"\nIndex\tName\tID\n";
for(int i=0;i<MAX;i++)
{
cout<<i;
cout<<"\t"<<rec[i].name;
cout<<"\t"<<rec[i].id;
cout<<"\n";
}
}
else
cout<<"No entries in the table(empty)";
}
void hashtable::find()
{
int id,k,i,p,flag=0,count=0;
cout<<"\nEnter ID to search: ";
cin>>id;
k=hashing(id);
for(i=k;i<MAX;i=(i+1)%MAX)
{
if(count==MAX)
break;
else
{
if(rec[i].id==id)
{
cout<<"ID is present at location: "<<i;
flag=1;
count++;
break;
}
else
count++;
}
}
if(flag==0)
cout<<"ID not found.";
}
int main()
{
hashtable obj;
int ch,ch1,no,size;
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)
{
case 1: if(count!=MAX)
{
cout<<"\nEnter ID:\t";
cin>>temph.id;
Page 2 of 5File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
cout<<"Enter name:\t";
cin>>temph.name;
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++ ads6.cpp
saiesh@saiesh :~$ ./a.out
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
11
Enter name:
Saiesh
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
12
Enter name:
Rohan
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
13
Enter name:
Payal
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
Enter ID:
Enter name:
29
Pratik
1
Page 3 of 5File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
22
Enter name:
Karishma
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
1
Enter ID:
19
Enter name:
Misba
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
ID
Name
0
19
Misba
1
11
Saiesh
2
12
Rohan
3
13
Payal
4
22
Karishma
5
-1
-
6
-1
-
7
-1
-
8
-1
-
9
29
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 ID to search: 11
ID is present at location: 1
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter ID to search: 29
ID is present at location: 9
Do you want to continue? y
Page 4 of 5File: /home/saiesh/ADS/Assignment No 6/ads6.cpp
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter ID to search: 19
ID is present at location: 0
Do you want to continue? y
HASH TABLE
1.Insert data
2.Display data
3.Find data
4.Exit
Enter your choice:
3
Enter ID to search: 26
ID 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