#include<iostream>
using namespace std;
#define size 10
class Record
{
string word;
string meaning;
int flag;
public:
Record()
{
word = "-";
meaning = "-";
flag = 0;
}
friend class Dictionary;
};
class Dictionary
{
int capacity;
int key;
public:
Dictionary() : capacity(0) {}
void input( Record table[] )
{
if( capacity == size )
{
cout<<"HASH TABLE FULL !!"<<endl;
return;
}
else
{
Record r;
cout<<"ENTER word : ";
getline(cin , r.word);
cin.ignore();
cout<<"ENTER MEANING : ";
cin>>r.meaning;
int index = hash(r.word);
while(table[index].flag == 1)
{
if(table[index].word == r.word)
{
cout<<" RECORD ALREADY EXIST !!"<<endl;
return;
}
index++;
index %= size;
}
table[index] = r;
table[index].flag = 1;
capacity++;
}
}
int hash( string word)
{
int sum = 0;
for(int i = 0 ; word[i] != '\0' ; i++)
{
sum += (int)word[i];
}
sum %= size;
return sum;
}
void display(Record table[])
{
cout<<":|:| HASH TABLE |:|:"<<endl;
for(int i = 0 ; i < size ; i++)
{
if(table[i].flag == 1)
{
cout<<endl<<"word : "<<table[i].word<<"\nMEANING : "<<table[i].meaning<<"\nPOSITION : "<<i<<endl;
}
}
}
void search(Record table[],string word)
{
key = hash(word);
int found = 0;
while( table[key].flag == 1)
{
if( table[key].word == word )
{
cout<<": RECORD FOUND :"<<endl;
cout<<"word : "<<table[key].word<<"\t MEANING : "<<table[key].meaning<<endl;
found = 1;
break;
}
else
{
key++;
key %= size;
}
}
if(found == 0)
{
cout<<".......RECORD NOT PRESENT......"<<endl;
}
}
void deletehash(Record table[], string word)
{
search(table,word);
if(table[key].flag == 1)
{
table[key].word = "empty";
table[key].meaning = "empty";
table[key].flag = 0;
cout<<":RECORD HAS BEEN DELETED:";
}
}
};
int main()
{
Record table[size];
Dictionary ph;
ph.input( table );
cin.ignore();
ph.input( table );
cin.ignore();
ph.input( table );
cin.ignore();
ph.input( table );
ph.display( table );
ph.search(table , "winter");
ph.deletehash(table,"winter");
ph.display(table);
return 0;
}
Comments
Post a Comment