# C++ Program to Implement Hash Tables Chaining with List Heads

A hash table is a data structure which is used to store key-value pairs. Hash function is used by hash table to compute an index into an array in which an element will be inserted or searched.

This is a C++ Program to Implement Hash Tables Chaining with List Heads.

## Algorithm

For insert:

Begin
Declare function Insert(int k, int v)
int hash_v = HashFunc(k)
if (ht[hash_v] == NULL)
else
while (en->n != NULL)
en = en->n
if (en->k == k)
en->v = v
else
End.

For Seach a key value:

Begin
Decla Function SearchKey(int k)
int hash_v = HashFunc(k)
if (ht[hash_v] == NULL)
return -1
else
while (en != NULL and en->k != k)
en= en->n
if (en== NULL)
return -1
else
return en->v
End

For Delete:

Begin
Declare Function Remove(int k)
int hash_v = HashFunc(k)
if (ht[hash_v] != NULL)
while (en->n != NULL and en->k != k)
p = en
en = en->n
if (en->k== k)
if (p == NULL)
delete en;
ht[hash_v] = n
else
delete en
p->n = n
End.

## Example Code

Live Demo

#include <iostream>
using namespace std;
const int T_S = 20;
public:
int k, v;
this->k = k;
this->v = v;
this->n = NULL;
}
};
class HashMapTable {
private:
public:
HashMapTable() {
for (int i = 0; i < T_S; i++) {
ht[i] = NULL;
}
}
int HashFunc(int k){
return k % T_S;
}
void Insert(int k, int v) {
int hash_v = HashFunc(k);
if (ht[hash_v] == NULL)
else {
while (en->n != NULL)
en = en->n;
if (en->k == k)
en->v = v;
else
}
}
int SearchKey(int k) {
int hash_v = HashFunc(k);
if (ht[hash_v] == NULL)
return -1;
else {
while (en != NULL && en->k != k)
en= en->n;
if (en == NULL)
return -1;
else
return en->v;
}
}
void Remove(int k) {
int hash_v = HashFunc(k);
if (ht[hash_v] != NULL) {
while (en->n != NULL && en->k != k) {
p = en;
en = en->n;
}
if (en->k == k) {
if (p == NULL) {
delete en;
ht[hash_v] = n;
}
else {
delete en;
p->n = n;
}
}
}
}
~HashMapTable() {
delete[] ht;
}
}
};
int main() {
HashMapTable hash;
int k, v;
int c;
while(1) {
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the key"<<endl;
cout<<"3.Delete element at a key"<<endl;
cout<<"4.Exit"<<endl;
cin>>c;
switch(c) {
case 1:
cout<<"Enter element to be inserted: ";
cin>>v;
cout<<"Enter key at which element to be inserted: ";
cin>>k;
hash.Insert(k, v);
break;
case 2:
cout<<"Enter key of the element to be searched: ";
cin>>k;
if (hash.SearchKey(k) == -1)
cout<<"No element found at key "<<k<<endl;
else {
cout<<"Elements at key "<<k<<" : ";
cout<<hash.SearchKey(k)<<endl;
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>k;
if (hash.SearchKey(k) == -1)
cout<<"Key "<<k<<" is empty"<<endl;
else {
hash.Remove(k);
cout<<"Entry Removed"<<endl;
}
break;
case 4:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}

## Output

1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter element to be inserted: 1
Enter key at which element to be inserted: 2
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter element to be inserted: 10
Enter key at which element to be inserted: 1
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter element to be inserted: 7
Enter key at which element to be inserted: 6
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter element to be inserted: 12
Enter key at which element to be inserted: 4
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter correct option
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter element to be inserted: 30
Enter key at which element to be inserted: 5
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter key of the element to be searched: 6
Elements at key 6 : 7
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter key of the element to be deleted: 1
Entry Removed
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 4