Skip to main content

C++ Program to Implement Hash Tables


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

OUTPUT

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

Comments

Popular posts from this blog

C++ Program for Circular Doubly Linked List

Today we are gonna tell you the C++ Program to demonstrate circular doubly linked list. The C++ program is successfully compiled and runs on any system.  Here is the code : #include<iostream> #include<cstdio> #include<cstdlib> using namespace std ;   /*  * Node Declaration  */ struct node { int info ; struct node * next ; struct node * prev ; } * start, * last ; int counter = 0 ; /*  * Class Declaration  */ class double_clist { public : node * create_node ( int ) ; void insert_begin ( ) ; void insert_last ( ) ; void insert_pos ( ) ; void delete_pos ( ) ; void search ( ) ; void update ( ) ; void display ( ) ; void reverse ( ) ; void sort ( ) ; double_clist ( ) { start = NULL ; last = NULL ; ...

Android secret codes

Just type these codes as it is in your phone and see the magic!!!! 1. Phone Information, Usage and Battery –  *#*#4636#*#* 2. IMEI Number – *#06# 3. Enter Service Menu On Newer Phones – *#0*# 4. Detailed Camera Information –  *#*#34971539#*#* 5. Backup All Media Files – *#*#273282*255* 663282*#*#* 6. Wireless LAN Test – *#*#232339#*#* 7. Enable Test Mode for Service –*#*#197328640#*#* 8. Back-light Test – *#*#0842#*#* 9. Test the Touchscreen – *#*#2664#*#* 10. Vibration Test – *#*#0842#*#* 11. FTA Software Version – *#*#1111#*#* 12. Complete Software and Hardware Info –*#12580*369# 13. Diagnostic Configuration – *#9090# 14. USB Logging Control – *#872564# 15. System Dump Mode – *#9900# 16. HSDPA/HSUPA Control Menu – *#301279# 17. View Phone Lock Status – *#7465625# 18. Reset the Data Partition to Factory State –*#*#7780#*#* 19. Format Your Device To Factory State(will delete everything on your phone) – *2767*3855# 20. Hidden Servic...

Finding IP address using facebook!!!!!

If you want to check for ip address of particular person on facebook or orkut or any other social site just invite them for a chat, so that your browser should connect to that system, than only when chat is ON open command prompt type the below command Netstat –an This will show you the  connected ip addresses, than from shown ip addresses search for suspicious ip address that is not the local connection address. Local connections normally start from 192.168.1.1 ranging to 192.168.1.255 Other netstat commands: -a Displays all connections and listening ports. -e Displays Ethernet statistics. This may be combined with the -s option. -n Displays addresses and port numbers in numerical form. -p proto Shows connections for the protocol specified by proto; proto may be TCP or UDP. -s option to display per-protocol statistics, proto may be TCP, UDP, or IP. -r Displays the routing table. -s Displays per-protocol statistics. By default, statistics are shown for TCP, UDP and IP; the -p op...