m2skills

lhash java

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