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 */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
# define max 10
 
typedef struct list
{
int data;
struct list  *next;
}node_type;
node_type *ptr[max],*root[max],*temp[max];
 
class Dictionary
{
public:
int index;
 
Dictionary();
void insert(int);
void search(int);
void delete_ele(int);
};
 
Dictionary::Dictionary()
{
index=-1;
for(int i=0;i<max;i++)
{
root[i]=NULL;
ptr[i]=NULL;
temp[i]=NULL;
}
}
 
void Dictionary::insert(int key)
{
index=int(key%max);
ptr[index]=(node_type*)malloc(sizeof(node_type));
ptr[index]->data=key;
if(root[index]==NULL)
{
root[index]=ptr[index];
root[index]->next=NULL;
temp[index]=ptr[index];
}
 
else
{
temp[index]=root[index];
while(temp[index]->next!=NULL)
temp[index]=temp[index]->next;
temp[index]->next=ptr[index];
}
}
 
void Dictionary::search(int key)
{
int flag=0;
index=int(key%max);
temp[index]=root[index];
while(temp[index]!=NULL)
{
if(temp[index]->data==key)
{
cout<<"\nSearch key is found!!";
flag=1;
break;
}
else temp[index]=temp[index]->next;
}
if (flag==0)
cout<<"\nsearch key not found.......";
}
 
void Dictionary::delete_ele(int key)
{
index=int(key%max);
temp[index]=root[index];
while(temp[index]->data!=key && temp[index]!=NULL)
{
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]);
}
 
main()
{
int val,ch,n,num;
char c;
Dictionary d;
 
do
{
cout<<"\nMENU:\n1.Create";
cout<<"\n2.Search for a value\n3.Delete an value";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
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++)
{
cin>>num;
d.insert(num);
}
break;
case 2:cout<<"\nEnter the element to be searched:";
cin>>n;
d.search(n);
case 3:cout<<"\nEnter the element to be deleted:";
cin>>n;
d.delete_ele(n);
break;
default:cout<<"\nInvalid choice....";
}
cout<<"\nEnter y to continue......";
cin>>c;
}while(c=='y');
getch();
}

OUTPUT

MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:1

Enter the number of elements to be inserted:8

Enter the elements to be inserted:10 4 5 8 7 12 6 1

Enter y to continue……y

MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:2

Enter the element to be searched:12

Search key is found!!
Enter the element to be deleted:1

1 has been deleted.
Enter y to continue……y

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

20 thoughts on “C++ program to implement all the functions of a dictionary (ADT) using hashing

  1. few errors in the program….
    corrections are..
    1.’i’ used in the ‘for loop’ in case 1 must be declared outside switch case!
    2. function ‘main’ must have return type.(void main)
    3.’break’ command missing in case 2.
    4.change #include to #include.

  2. Write a program in c++ to provide simple inventory system for a bookshop. It should hold the current list of stock, allow updates, additions, deletions and so on, and keep records of all books sold. It should be possible to display stock and sales reports.

  3. Useful information. Fortunate me I discovered your site by accident,
    and I am surprised why this coincidence didn’t came about earlier!
    I bookmarked it.

  4. This аrticle is genuinely a good one iit helps
    new the web visitors, who are wishing foг blogging.

  5. I really like what you guys are usually up too.

    Such clever work and coverage! Keep up the great works guys I’ve incorporated you guys
    to blogroll.

  6. There is no need to purchase bottled water, which is expensive; you can store your own fresh drinking water for purposes of drinking in vertical storage tanks.
    Reed beds present sludge dewatering by plant consumption, evapotranspiration, and drainage.

    While both parents were charged, one of the charges that the boy’s mother is
    facing is more serious than what the boy’s father is facing, according to WUSA-9 on April 3.

  7. Those people who have been online for a while know that website ideas
    that make money online really worth paying attention. If these eight steps seem overwhelming, welcome to the club.
    You will also find 30 free tools, and software programs
    to help you build a successfull Internet Business.

  8. Those people who have been online for a while know that website ideas
    that make money online really worth paying attention. I set an example of such information to demonstrate the
    formatting of a typical contact page as shown below:
    . You will also find 30 free tools, and software programs to help you build a successfull Internet Business.

  9. This is how quite a few website owners make substantial 6
    and 7 figure incomes each year. Small claims court cases are much cheaper than superior court cases for both the plaintiff (the person doing the
    suing) and the defendant (the person being sued) because the parties are not allowed to have any attorneys represent them and other rules
    that simplify the lawsuit process, making the whole thing
    much cheaper, faster, and easier. The website templates are well-equipped with HTML
    coding which could result into better looking websites in minor budget also.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in