m2skills

dhash cpp

Apr 4th, 2017
4,761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. // Program to implement Double Hashing
  2. #include<iostream>
  3. #include<math.h>
  4. #define SIZE 13
  5. using namespace std;
  6.  
  7. class hashTable{
  8.    
  9.     private:
  10.         int table[SIZE], elementCount, num;
  11.    
  12.     public:
  13.    
  14.         // initialize hash Table
  15.         hashTable(){
  16.             for(int i=0; i < SIZE; i++){
  17.                 table[i] = 0;
  18.             }
  19.             elementCount = 0;
  20.             num = 11;
  21.         }
  22.        
  23.         // method that checks if the hash table is full or not
  24.         bool isFull(){
  25.             if(elementCount == SIZE){
  26.                 return true;
  27.             }else{
  28.                 return false;
  29.             }
  30.         }
  31.        
  32.        
  33.         // method that returns position for a given element
  34.         int h1(int element){
  35.             return element % SIZE;
  36.         }  
  37.        
  38.         int h2(int element){
  39.             return element % num;
  40.         }
  41.        
  42.         // method that finds the next empty position using quadratic probing
  43.         int doubleHashing(int element){
  44.             bool posFound = false;
  45.             int newPosition = -1;
  46.             int limit = 50, i = 2;
  47.             while(i <= 50){
  48.                 newPosition = (i*this->h1(element) + this->h2(element)) % SIZE;
  49.                 if(this->table[newPosition] == 0){
  50.                     posFound = true;
  51.                     break;
  52.                 }
  53.                 else{
  54.                     i++;
  55.                 }
  56.             }
  57.             if(posFound){
  58.                 return newPosition;
  59.             }else{
  60.                 return -1;
  61.             }  
  62.         }
  63.        
  64.         // method that inserts element inside the hash table
  65.         void insert(int element){
  66.             int position;
  67.             // checking if the table is full
  68.             if(this->isFull()){
  69.                 cout<<"\nHash Table Full";
  70.                 return;
  71.             }  
  72.            
  73.             bool isStored = false;
  74.            
  75.             position = this->h1(element);
  76.                
  77.             // checking if the position is empty
  78.             if(table[position] == 0){
  79.                 table[position] = element;
  80.                 cout<<"\nElement " <<element<<" stored at position " <<position;
  81.                 isStored = true;
  82.                 elementCount += 1;
  83.             }  
  84.            
  85.             // collision occured hence we do linear probing
  86.             else{
  87.                 cout<<"\nCollision has occured for element " <<element<<" at position " <<position<<" finding new Position.";
  88.                 position = doubleHashing(element);
  89.                 if(position != -1){
  90.                     this->table[position] = element;
  91.                     elementCount += 1;
  92.                     cout<<"\nElement " <<element<<" stored at position " <<position;
  93.                 }
  94.                
  95.             }  
  96.             return;
  97.         }  
  98.  
  99.         // method that searches for an element in the table
  100.         // returns position of element if found
  101.         // else returns false
  102.         int search(int element){
  103.             bool found = false;
  104.            
  105.             int position = this->h1(element);
  106.            
  107.             if(table[position] == element){
  108.                 found = true;
  109.                 return position;
  110.             }  
  111.            
  112.             // if element is not found at position returned hash function
  113.             // then we search element using quadratic probing
  114.             else{
  115.                 int limit = 50, i = 1;
  116.                 int newPosition = -1;
  117.                 while(i <= 50){
  118.                     newPosition = (i*this->h1(element) + this->h2(element)) % SIZE;;
  119.                     if(this->table[newPosition] == element){
  120.                         found = true;
  121.                         break;
  122.                     }
  123.                     else{
  124.                         i++;
  125.                     }
  126.                 }
  127.                 if(found){
  128.                     return newPosition;
  129.                 }
  130.                 else{
  131.                     cout<<"Element not found";
  132.                     return -1;
  133.                 }
  134.             }  
  135.         }  
  136.        
  137.        
  138.         // method to remove an element from the table      
  139.         void remove(int element){
  140.             int position = this->search(element);
  141.             if(position != -1){
  142.                 table[position] = 0;
  143.                 cout<<"\nElement " <<element<<" is Deleted";
  144.                 this->elementCount -= 1;
  145.             }else{
  146.                 cout<<"\nElement is not present in the Hash Table";
  147.             }
  148.             return;
  149.         }  
  150.        
  151.         // method to display the hash table
  152.         void display(){
  153.             for (int i = 0; i < SIZE; i++){
  154.                 cout<<"\n"<<i<<" = "<<table[i];
  155.             }  
  156.             cout<<"\nThe number of element is the Table are : " <<this->elementCount;
  157.         }  
  158. };     
  159.  
  160. int main(){
  161.     // main function
  162.     hashTable table1;
  163.  
  164.     // storing elements in table
  165.     table1.insert(12);
  166.     table1.insert(26);
  167.     table1.insert(31);
  168.     table1.insert(17);
  169.     table1.insert(90);
  170.     table1.insert(28);
  171.     table1.insert(88);
  172.     table1.insert(40);
  173.     table1.insert(77);      // element that causes collision at position 0
  174.  
  175.     // displaying the Table
  176.     table1.display();
  177.     cout<<"\n";
  178.  
  179.     // printing position of elements
  180.     cout<<"\nThe position of element 31 is : " <<table1.search(31);
  181.     cout<<"\nThe position of element 28 is : " <<table1.search(28);
  182.     cout<<"\nThe position of element 90 is : " <<table1.search(90);
  183.     cout<<"\nThe position of element 77 is : " <<table1.search(77);
  184.     cout<<"\nThe position of element 1 is : " <<table1.search(1);
  185.  
  186.     cout<<"\n";
  187.  
  188.     table1.remove(90);
  189.     table1.remove(12);
  190.  
  191.     table1.display();
  192. }
Add Comment
Please, Sign In to add comment