You are on page 1of 5

#include<iostream>

#include<string.h>
using namespace std;
class Node{
public:
int chain;
string mean, word;
}obj[10];
int hash_func(string wd){
int key = 0;
for(int i = 0; i<wd.size(); i++){
key += wd[i];
}
return key%10;
}
void collision(int key, string wd, string mn){
int i = 1;
while(((key%i)%10)<10){
if(obj[(key+i)%10].word == wd){
obj[(key+i)%10].word = wd;
obj[(key+i)%10].mean = mn;
obj[(key+i-1)%10].chain = (key+i)%10;
break;
}
else{
i++;
}
}
}
void hash_ini(){
for(int i=0; i<10; i++){
obj[i].word = "-";
obj[i].mean = "-";
obj[i].chain = -1;
}
}
void hash_table(){
string wd, mn;
cout<<"\nEnter a word: ";
cin>>wd;
cout<<"\n Enter meaning: ";
cin>>mn;
cout<<endl;
int hash_key = hash_func(wd);
if(obj[hash_key].word == "-"){
obj[hash_key].word = wd;
obj[hash_key].mean = mn;
}
else{
collision(hash_key, wd, mn);
}
}
void display(){
cout<<"Index \t Word \t Meaning \t Chain \n";
for(int i=0; i<10; i++){
cout<<i<<"\t"<<obj[i].word<<"\t"<<obj[i].mean<<"\t"<<obj[i].chain<<endl;
}
}
void del_key(string word){
int key = hash_func(word);
if(obj[key].word == word){
obj[key].word = "-";
obj[key].mean = "-";
obj[key].chain = -1;
}
else if(obj[key].word!=word){
int target = obj[key].chain;
while(true){
if(obj[target].word == word){
obj[target-1].chain = obj[target].chain;
obj[target].word = "-";
obj[target].mean = "-";
obj[target].chain = -1;
cout<<"Deletion is Successful"<<endl;
break;
}
target = obj[target].chain;
}
}
else{
cout<<"Word Not Found!!!"<<endl;
}
}
void find(string word){
int key = hash_func(word);
if(obj[key].word == word){
cout<<"Found the word"<<endl;
cout<<obj[key].word<<"\t"<<obj[key].mean<<endl;
}
else if(obj[key].chain!=-1){
int target = obj[key].chain;
while (true){
if(obj[target].word ==word){
cout<<"Found the word"<<endl;
cout<<obj[target].word<<"\t"<<obj[target].mean<<endl;
break;
}
target = obj[target].chain;
}
}
else{
cout<<"Not Found!!"<<endl;
}
}
int main(){
int ch;
string delwd;
string src;
hash_ini();
do{
cout<<"********Enter the choice*********"<<endl;
cout<<"1.Insert \n2.Display \n3.Delete \n4.Find \n5.Exit\n";
cout<<"Enter your choice: ";
cin>>ch;
cout<<endl;
switch(ch){
case 1:
cout<<"Enter no of entries you want to make: ";
int n;
cin>>n;
for(int i=0;i<n;i++){
hash_table();
}
break;
case 2:
display();
break;
case 3:
cout<<"Enter the word you want to delete: ";
cin>>delwd;
del_key(delwd);
break;
case 4:
cout<<"Enter the word you want to find: ";
cin>>src;
find(src);
break;
case 5:
exit(0);
break;
}
}while(ch<=5);
return 0;
}

Output:
********Enter the choice*********
1.Insert
2.Display
3.Delete
4.Find
5.Exit
Enter your choice: 1

Enter no of entries you want to make: 5

Enter a word: AAA

Enter meaning: aaa

Enter a word: RRR

Enter meaning: rrr

Enter a word: TTT

Enter meaning: ttt


Enter a word: GGG

Enter meaning: ggg

Enter a word: BBB

Enter meaning: bbb

********Enter the choice*********


1.Insert
2.Display
3.Delete
4.Find
5.Exit
Enter your choice: 2

Index Word Meaning Chain


0 - - -1
1 - - -1
2 TTT ttt -1
3 GGG ggg -1
4 - - -1
5 AAA aaa -1
6 RRR rrr -1
7 - - -1
8 BBB bbb -1
9 - - -1
********Enter the choice*********
1.Insert
2.Display
3.Delete
4.Find
5.Exit
Enter your choice: 3

Enter the word you want to delete: GGG


********Enter the choice*********
1.Insert
2.Display
3.Delete
4.Find
5.Exit
Enter your choice: 4

Enter the word you want to find: RRR


Found the word
RRR rrr
********Enter the choice*********
1.Insert
2.Display
3.Delete
4.Find
5.Exit
Enter your choice: 2

Index Word Meaning Chain


Index Word Meaning Chain
0 - - -1
1 - - -1
2 TTT ttt -1
3 - - -1
4 - - -1
5 AAA aaa -1
6 RRR rrr -1
7 - - -1
8 BBB bbb -1
9 - - -1
********Enter the choice*********
1.Insert
2.Display
3.Delete
4.Find
5.Exit
Enter your choice: 5

You might also like