C++ Program to Implement Hash Tables

«
»
This C++ Program demonstrates operations on Hash Tables

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

$ g++ hash_table.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: 12
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: 24
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: 36
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: 48
Enter key at which element to be inserted: 4
 
----------------------
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: 60
Enter key at which element to be inserted: 5
 
----------------------
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: 3
Element at key 3 : 36
 
----------------------
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: 5
Element at key 5 : 60
 
----------------------
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: 4
Element 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: 4
No element found at key 4
 
----------------------
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
Element at key 2 : 24
 
----------------------
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.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

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 & technical discussions at Telegram SanfoundryClasses.