6. Implement all the functions of a dictionary (ADT) using hashing.Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must be uniqueStandard Operations: Insert(key, value), Find(key), Delete(key)

#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

Popular posts from this blog

2. Beginning with an empty binary search tree, Construct binary search tree by inserting the values in the order given. After constructing a binary tree -i.Insert new nodeii.Find number of nodes in longest path iii.Minimum data value found inthe tree iv.Change a tree so that the roles of the left and right pointers are swapped at every node v.Search a value

1. A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method.

4. Write a function to get the number of vertices in an undirected graph and its edges. You may assume thatno edge is input twice. i.Use adjacency list representation of the graph and find runtime of the functionii.Use adjacency matrix representation of the graph and find runtime of the function