This C++ Program demonstrates operations on Hash Tables Chaining with List Heads.
Here is source code of the C++ Program to demonstrate Hash Tables Chaining with List Heads. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Implement Hash Tables Chaining with List Heads
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdlib>
using namespace std;
const int TABLE_SIZE = 5;
/*
* Link List Class Declaration
*/
class LinkedHash
{
public:
int key, value;
LinkedHash *next;
LinkedHash(int key, int value)
{
this->key = key;
this->value = value;
this->next = NULL;
}
};
/*
* HashMap Class Declaration
*/
class HashMap
{
private:
LinkedHash **htable;
public:
HashMap()
{
htable = new LinkedHash*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
{
htable[i] = NULL;
}
}
~HashMap()
{
for (int i = 0; i < TABLE_SIZE; i++)
{
if (htable[i] != NULL)
{
LinkedHash *prev = NULL;
LinkedHash *entry = htable[i];
while (entry != NULL)
{
prev = entry;
entry = entry->next;
delete prev;
}
}
delete[] htable;
}
}
/*
* Hash Function
*/
int HashFunc(int key)
{
return key % TABLE_SIZE;
}
/*
* Insert Element at a key
*/
void Insert(int key, int value)
{
int hash_val = HashFunc(key);
if (htable[hash_val] == NULL)
htable[hash_val] = new LinkedHash(key, value);
else
{
LinkedHash *entry = htable[hash_val];
while (entry->next != NULL)
entry = entry->next;
if (entry->key == key)
entry->value = value;
else
entry->next = new LinkedHash(key, value);
}
}
/*
* Search Element at a key
*/
int Find(int key)
{
int hash_val = HashFunc(key);
if (htable[hash_val] == NULL)
return -1;
else
{
LinkedHash *entry = htable[hash_val];
while (entry != NULL && entry->key != key)
entry = entry->next;
if (entry == NULL)
return -1;
else
return entry->value;
}
}
/*
* Delete Element at a key
*/
void Delete(int key)
{
int hash_val = HashFunc(key);
if (htable[hash_val] != NULL)
{
LinkedHash *entry = htable[hash_val];
LinkedHash *prev = NULL;
while (entry->next != NULL && entry->key != key)
{
prev = entry;
entry = entry->next;
}
if (entry->key == key)
{
if (prev == NULL)
{
LinkedHash *next = entry->next;
delete entry;
htable[hash_val] = next;
}
else
{
LinkedHash *next = entry->next;
delete entry;
prev->next = next;
}
}
}
}
};
/*
* Main Contains Menu
*/
int main()
{
HashMap hash;
int key, value;
int choice;
while(1)
{
cout<<"\n----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<"\n----------------------"<<endl;
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;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
hash.Insert(key, value);
break;
case 2:
cout<<"Enter key of the element to be searched: ";
cin>>key;
if (hash.Find(key) == -1)
cout<<"No element found at key "<<key<<endl;
else
{
cout<<"Elements at key "<<key<<" : ";
cout<<hash.Find(key)<<endl;
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>key;
if (hash.Find(key) == -1)
cout<<"Key "<<key<<" is empty, nothing to delete"<<endl;
else
{
hash.Delete(key);
cout<<"Entry Deleted"<<endl;
}
break;
case 4:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}
$ g++ hash_listheads.cpp $ a.out ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 100 Enter key at which element to be inserted: 1 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 200 Enter key at which element to be inserted: 2 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 300 Enter key at which element to be inserted: 3 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 600 Enter key at which element to be inserted: 6 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 700 Enter key at which element to be inserted: 7 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 800 Enter key at which element to be inserted: 8 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 2 Enter key of the element to be searched: 2 Elements at key 2 : 200 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 2 Enter key of the element to be searched: 7 Elements at key 7 : 700 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 2 Enter key of the element to be searched: 9 No element found at key 9 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 3 Enter key of the element to be deleted: 7 Entry Deleted ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 2 Enter key of the element to be searched: 7 No element found at key 7 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 2 Enter key of the element to be searched: 2 Elements at key 2 : 200 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 1111 Enter key at which element to be inserted: 7 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 2 Enter key of the element to be searched: 7 Elements at key 7 : 1111 ---------------------- Operations on Hash Table ---------------------- 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 4 ------------------ (program exited with code: 1) Press return to continue
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Computer Science MCQs
- Practice Design & Analysis of Algorithms MCQ
- Check Data Structure Books
- Practice Programming MCQs
- Check Programming Books