Write A C++ Program To Implement All The Functions of A Dictionary (ADT) Using Hashing
Write A C++ Program To Implement All The Functions of A Dictionary (ADT) Using Hashing
com
/* Write a C++ program to implement all the functions of a dictionary (ADT) using
hashing */
#include<iostream>
#include<conio.h>
omm
#include<stdlib.h>
using namespace std;
# define max 10
i.c.co
typedef struct list
{
int data;
struct list *next;
}node_type;
ooggi
node_type *ptr[max],*root[max],*temp[max];
class Dictionary
{
public:
ntyty
int index;
Dictionary();
void insert(int);
ddeen
void search(int);
void delete_ele(int);
};
Dictionary::Dictionary()
{
sstutu
index=-1;
for(int i=0;i<max;i++)
{
root[i]=NULL;
ptr[i]=NULL;
ww. .
temp[i]=NULL;
}
}
wwww
www.studentyogi.com
www.studentyogi.com
root[index]->next=NULL;
temp[index]=ptr[index];
}
else
m
{
temp[index]=root[index];
while(temp[index]->next!=NULL)
co
temp[index]=temp[index]->next;
temp[index]->next=ptr[index];
}
}
gi.
void Dictionary::search(int key)
{
int flag=0;
index=int(key%max);
temp[index]=root[index];
while(temp[index]!=NULL)
{
tyo
if(temp[index]->data==key)
{
cout<<"\nSearch key is found!!";
en
flag=1;
break;
}
else temp[index]=temp[index]->next;
d
}
if (flag==0)
stu
index=int(key%max);
temp[index]=root[index];
while(temp[index]->data!=key && temp[index]!=NULL)
{
ww
ptr[index]=temp[index];
temp[index]=temp[index]->next;
}
ptr[index]->next=temp[index]->next;
cout<<"\n"<<temp[index]->data<<" has been deleted.";
temp[index]->data=-1;
temp[index]=NULL;
free(temp[index]);
www.studentyogi.com
www.studentyogi.com
}
m
co
main()
{
int val,ch,n,num;
char c;
gi.
Dictionary d;
do
{
cout<<"\nMENU:\n1.Create";
tyo
cout<<"\n2.Search for a value\n3.Delete an value";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
en
case 1:cout<<"\nEnter the number of elements to be inserted:";
cin>>n;
cout<<"\nEnter the elements to be inserted:";
for(int i=0;i<n;i++)
d
{
cin>>num;
stu
d.insert(num);
}
break;
case 2:cout<<"\nEnter the element to be searched:";
cin>>n;
w.
d.search(n);
case 3:cout<<"\nEnter the element to be deleted:";
cin>>n;
d.delete_ele(n);
ww
break;
default:cout<<"\nInvalid choice....";
}
cout<<"\nEnter y to continue......";
cin>>c;
}while(c=='y');
getch();
}
www.studentyogi.com
www.studentyogi.com
m
co
gi.
OUTPUT
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:1
tyo
Enter the number of elements to be inserted:8
MENU:
d
1.Create
2.Search for a value
stu
3.Delete an value
Enter your choice:2
Enter y to continue......y
www.studentyogi.com