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