[go: up one dir, main page]

0% found this document useful (0 votes)
5 views10 pages

Hash Table

This document contains a C++ implementation of a hash table class with methods for inserting, deleting, and displaying elements. It includes functionality to check if the hash table is full or empty, and uses linear probing for collision resolution. The main function provides a menu-driven interface for user interaction with the hash table.

Uploaded by

subashini.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Hash Table

This document contains a C++ implementation of a hash table class with methods for inserting, deleting, and displaying elements. It includes functionality to check if the hash table is full or empty, and uses linear probing for collision resolution. The main function provides a menu-driven interface for user interaction with the hash table.

Uploaded by

subashini.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

PROGRAM:

*\ HASH TABLE *\

#include<iostream.h>
#include<conio.h>
enum bool{false,true};
template<class T>
class hash
{
T *tab;
int size;
public:
hash(int s);
~hash();
void insert();
void del();
void display();
bool isfull();
bool isempty();
};
template<class T>
hash<T>::hash(int s)
{
size=s;
tab=new T[size];
for(int i=0;i<size;i++)
tab[i]=-1;//tab[i]=NULL;
}
template<class T>
hash<T>::~hash()
{
delete []tab;
}
template<class T>
bool hash<T>::isfull()
{
for(int i=0;i<size;i++)
if(tab[i]==-1)
return false;
return true;
}
template<class T>
bool hash<T>::isempty()
{
for(int i=0;i<size;i++)
if(tab[i]!=-1)
return false;
return true;
}
template<class T>
void hash<T>::insert()
{
T e;
int p;
cout<<"\n enter the element::";
cin>>e;
if(!isfull())
{
p=(int)e%size;
do
{
if((tab[p]==-1))
{
tab[p]=e;
break;
}
p++;
if(p==size)
p=0;
}while(1);
}
else
{
cout<<"\n* hashtable is full *";
}
}
template<class T>
void hash<T>::del()
{
T e;
int p,cnt,st=0;
cout<<"\n enter the element:";
cin>>e;
if(!isempty())
{
p=(int)e%size;
for(cnt=0;cnt<size;cnt++)
{
if((tab[p]==e))
{
tab[p]=-1;
cout<<"\n element has been deleted...";
st=1;
break;
}
p++;
if(p==size)
p=0;
}
if(st==0)
cout<<"\n element not found...";
}
else
cout<<"\n* hashtable is empty *";
}
template<class T>
void hash<T>::display()
{
cout<<"\n\tHASH TABLE";
cout<<"\n------------------------";
cout<<"\nposition\telement";
cout<<"\n------------------------";
for(int i=0;i<size;i++)
cout<<"\n"<<i<<"\t\t"<<tab[i];
cout<<"\n------------------------";
}
void main()
{
int ch,s;
int st=1;
clrscr();
cout<<"\nenter the size of the hash table:";
cin>>s;
hash<float>ht(s);
while(st)
{
clrscr();
cout<<"\n*** HASH TABLE MENU ***";
cout<<"\n1.insert\n2.delete\n3.display\n4.exit\n";
cout<<"\n enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
ht.insert();
break;
case 2:
ht.del();
break;
case 3:
ht.display();
break;
case 4:
st=0;
break;
}
cout<<"\n press any key to continue...";
getch();
}
}

OUTPUT:

You might also like