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

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.

  1. /*
  2.  * C++ Program to Implement Hash Tables Chaining with List Heads
  3.  */
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <vector>
  10. #include <cstdlib>
  11. using namespace std;
  12. const int TABLE_SIZE = 5;
  13. /*
  14.  * Link List Class Declaration
  15.  */
  16. class LinkedHash
  17. {
  18. 	public:
  19.         int key, value;
  20.         LinkedHash *next;
  21.         LinkedHash(int key, int value)
  22.         {
  23.             this->key = key;
  24.             this->value = value;
  25.             this->next = NULL;
  26.         }
  27. };
  28. /*
  29.  * HashMap Class Declaration
  30.  */
  31. class HashMap
  32. {
  33.     private:
  34.         LinkedHash **htable;
  35.     public:
  36.         HashMap()
  37.         {
  38.             htable = new LinkedHash*[TABLE_SIZE];
  39.             for (int i = 0; i < TABLE_SIZE; i++)
  40.             {
  41.                 htable[i] = NULL;
  42.             }
  43.         }
  44.         ~HashMap()
  45.         {
  46.             for (int i = 0; i < TABLE_SIZE; i++)
  47.             {
  48.                 if (htable[i] != NULL)
  49.                 {
  50.                     LinkedHash *prev = NULL;
  51.                     LinkedHash *entry = htable[i];
  52.                     while (entry != NULL)
  53.                     {
  54.                         prev = entry;
  55.                         entry = entry->next;
  56.                         delete prev;
  57.                     }
  58.                 }
  59.                 delete[] htable;
  60.             }
  61.         }
  62.         /*
  63. 	     * Hash Function
  64.          */
  65.         int HashFunc(int key)
  66. 		{
  67.             return key % TABLE_SIZE;
  68.         }
  69.         /*
  70. 	     * Insert Element at a key
  71.          */
  72.         void Insert(int key, int value)
  73.         {
  74.             int hash_val = HashFunc(key);
  75.             if (htable[hash_val] == NULL)
  76.                 htable[hash_val] = new LinkedHash(key, value);
  77.             else
  78.             {
  79.                 LinkedHash *entry = htable[hash_val];
  80.                 while (entry->next != NULL)
  81.                     entry = entry->next;
  82.                     if (entry->key == key)
  83.                         entry->value = value;
  84.                     else
  85.                         entry->next = new LinkedHash(key, value);
  86.             }
  87.         }
  88.         /*
  89. 	     * Search Element at a key
  90.          */
  91.         int Find(int key)
  92.         {
  93.             int hash_val = HashFunc(key);
  94.             if (htable[hash_val] == NULL)
  95.                 return -1;
  96.             else
  97.             {
  98.                 LinkedHash *entry = htable[hash_val];
  99.                 while (entry != NULL && entry->key != key)
  100.                     entry = entry->next;
  101.                 if (entry == NULL)
  102.                     return -1;
  103.                 else
  104.                     return entry->value;
  105.             }
  106.         }
  107.         /*
  108. 	     * Delete Element at a key
  109.          */
  110.         void Delete(int key)
  111.         {
  112.             int hash_val = HashFunc(key);
  113.             if (htable[hash_val] != NULL)
  114.             {
  115.                 LinkedHash *entry = htable[hash_val];
  116.                 LinkedHash *prev = NULL;
  117.                 while (entry->next != NULL && entry->key != key)
  118.                 {
  119.                     prev = entry;
  120.                     entry = entry->next;
  121.                 }
  122.                 if (entry->key == key)
  123.                 {
  124.                     if (prev == NULL)
  125.                     {
  126.                         LinkedHash *next = entry->next;
  127.                         delete entry;
  128.                         htable[hash_val] = next;
  129.                     }
  130.                     else
  131.                     {
  132.                         LinkedHash *next = entry->next;
  133.                         delete entry;
  134.                         prev->next = next;
  135.                     }
  136.                 }
  137.             }
  138.         }
  139. };
  140. /*
  141.  * Main Contains Menu
  142.  */
  143. int main()
  144. {
  145.     HashMap hash;
  146.     int key, value;
  147.     int choice;
  148.     while(1)
  149.     {
  150.         cout<<"\n----------------------"<<endl;
  151.         cout<<"Operations on Hash Table"<<endl;
  152.         cout<<"\n----------------------"<<endl;
  153.         cout<<"1.Insert element into the table"<<endl;
  154.         cout<<"2.Search element from the key"<<endl;
  155.         cout<<"3.Delete element at a key"<<endl;
  156.         cout<<"4.Exit"<<endl;
  157.         cout<<"Enter your choice: ";
  158.         cin>>choice;
  159.         switch(choice)
  160.         {
  161.         case 1:
  162.             cout<<"Enter element to be inserted: ";
  163.             cin>>value;
  164.             cout<<"Enter key at which element to be inserted: ";
  165.             cin>>key;
  166.             hash.Insert(key, value);
  167.             break;
  168.         case 2:
  169.             cout<<"Enter key of the element to be searched: ";
  170.             cin>>key;
  171.             if (hash.Find(key) == -1)
  172.             cout<<"No element found at key "<<key<<endl;
  173.             else
  174.             {
  175.                 cout<<"Elements at key "<<key<<" : ";
  176.                 cout<<hash.Find(key)<<endl;
  177.             }
  178.             break;
  179.         case 3:
  180.             cout<<"Enter key of the element to be deleted: ";
  181.             cin>>key;
  182.             if (hash.Find(key) == -1)
  183.                 cout<<"Key "<<key<<" is empty, nothing to delete"<<endl;
  184.             else
  185.             {
  186.                 hash.Delete(key);
  187.                 cout<<"Entry Deleted"<<endl;
  188.             }
  189.             break;
  190.         case 4:
  191.             exit(1);
  192.         default:
  193.            cout<<"\nEnter correct option\n";
  194.        }
  195.     }
  196.     return 0;
  197. }

$ 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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.