m2skills

lhash c++

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