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

How to create Bootable USB using Rufus

Rufus is a great software that allows you to create a bootable USB drive using an .ISO file.Its Ideal for installing programs and software on windows for whom CD drive are not available. In order to create a bootable USB drive you will need three things : 1 USB drive 2 Your .ISO file which you want to boot your USB with. 3. Rufus Let's get going!! STEP 1 First of all download Rufus , you can download it from their site -  https://rufus.akeo.ie/ Download the latest version (right now its Rufus 2.9). You don't need to install it , it runs directly STEP 2 : Inser the USB drive and open Rufus , make sure you take backup of all files in your USB drive because Rufus will format the USB before booting it. STEP 3: Once you have started Rufus check for all desired things. Make sure device field is set to the correct device(device that you want to boot i.e USB) Also Make Sure "Create a bootable disk " option is ticked else i...

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...

Software Testing through Fuzzing

Fuzz testing or fuzzing is a software testing technique used to discover coding errors and security loopholes in software, operating systems or networks by inputting massive amounts of random data, called fuzz, to the system in an attempt to make it crash . Fuzzing is  often automated or semi-automated, that  involves providing invalid, unexpected, or random data to the inputs of a computer program. The program is then monitored for exceptions such as crashes, or failing built-in code assertions or for finding potential memory leaks. Fuzzing technique is commonly used to test for security problems in software or computer systems ans also used to discover coding errors and security loopholes in software, operating systems  or networks by inputting massive amounts of random data, called fuzz, to the system  in an attempt to make it crash. If a vulnerability is found, a tool called a fuzz tester (or fuzzer), indicates potential causes. There are two forms ...