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

SQL Injection ,Hacking PHP 4.4 sites in seconds

Today I am going to teach you how to hack a certain type of websites with very least efforts. Websites with PHP  4.4 have a SQL injection vulnerability in them which makes their Admin control panel easily accessible,and in just few steps you will access the admin's account of that website. Remember,this tutorial is applicable on PHP 4.4 machines with Apache running in parallel with them. Also,since I will be hacking REAL websites,I will not be displaying their URL’s or else I will be sued!!!. Also this tutorial is only for educational purpose. Here we go!!! Step 1 – Search for them Yep,make a Google dork to find sites running Apache and PHP 4.4 . Its quite easy.You can do this by searching inurl:adminlogin . Step 2 – Scan them Start by scanning them using Nmap ,Do and intense scan and find the open ports. If you find port 2000 open,then you have almost got it. most websites running PHP4.4 have this port for admin login. Now just login using port 2000 ie - ...

Bellmanford Algorithm C++ Program

The  Bellman–Ford algorithm  is an  algorithm  that computes  shortest paths  from a single source  vertex  to all of the other vertices in a  weighted digraph . It is slower than  Dijkstra's algorithm  for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers. Here is the Source Code: #include<iostream> #include<stdio.h> using namespace std; #include<conio.h> #define INFINITY 999 struct node {     int cost;     int value;     int from; }a[5]; void addEdge(int am[][5],int src,int dest,int cost) {      am[src][dest] = cost;      return; } void bell(int am[][5]) {     int i, j, k, c = 0, temp;     a[0].cost = 0;     a[0].from = 0;     a[0].value = 0;     for (i = 1; i < 5; i++)     { ...