Java Program to Implement Hash Tables Chaining with Doubly Linked Lists

«
»
This is a Java Program to implement hash tables chaining with Doubly Linked Lists. A hash table (also hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found. In order to prevent collision, hash tables are chained with another data structure ( Doubly Linked List in this case ).

Here is the source code of the Java program to implement hash tables chaining with Doubly Linked Lists. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  *    Java Program to Implement Hash Tables Chaining with Doubly Linked Lists 
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. /* Node for doubly linked list */
  8. class DLLNode
  9. {
  10.     DLLNode next, prev;
  11.     int data;
  12.  
  13.     /* Constructor */
  14.     public DLLNode(int x)
  15.     {
  16.         data = x;
  17.         next = null;
  18.         prev = null;
  19.     }
  20. }
  21.  
  22. /* Class HashTableChainingDoublyLinkedList */
  23. class HashTableChainingDoublyLinkedList
  24. {
  25.     private DLLNode[] table;
  26.     private int size ;
  27.  
  28.     /* Constructor */
  29.     public HashTableChainingDoublyLinkedList(int tableSize)
  30.     {
  31.         table = new DLLNode[ nextPrime(tableSize) ];
  32.         size = 0;
  33.     }
  34.     /* Function to check if hash table is empty */
  35.     public boolean isEmpty()
  36.     {
  37.         return size == 0;
  38.     }
  39.     /* Function to clear hash table */
  40.     public void makeEmpty()
  41.     {
  42.         int l = table.length;
  43.         table = new DLLNode[l];
  44.         size = 0;
  45.     }
  46.     /* Function to get size */
  47.     public int getSize()
  48.     {
  49.         return size;
  50.     }
  51.     /* Function to insert an element */
  52.     public void insert(int val)
  53.     {
  54.         size++;
  55.         int pos = myhash(val);        
  56.         DLLNode nptr = new DLLNode(val);
  57.         DLLNode start = table[pos];                
  58.         if (table[pos] == null)
  59.             table[pos] = nptr;            
  60.         else
  61.         {
  62.             nptr.next = start;
  63.             start.prev = nptr;
  64.             table[pos] = nptr;
  65.         }    
  66.     }
  67.     /* Function to remove an element */
  68.     public void remove(int val)
  69.     {
  70.         try
  71.         {
  72.             int pos = myhash(val);    
  73.             DLLNode start = table[pos];
  74.             DLLNode end = start;
  75.             if (start.data == val)
  76.             {
  77.                 size--;
  78.                 if (start.next == null)
  79.                 {
  80.                     table[pos] = null;
  81.                     return;
  82.                 }                
  83.                 start = start.next;
  84.                 start.prev = null;
  85.                 table[pos] = start;
  86.                 return;
  87.             }
  88.  
  89.             while (end.next != null && end.next.data != val)
  90.                 end = end.next;
  91.             if (end.next == null)
  92.             {
  93.                 System.out.println("\nElement not found\n");
  94.                 return;
  95.             }
  96.             size--;
  97.             if (end.next.next == null)
  98.             {
  99.                 end.next = null;
  100.                 return;
  101.             }
  102.             end.next.next.prev = end;
  103.             end.next = end.next.next;
  104.  
  105.             table[pos] = start;
  106.         }
  107.         catch (Exception e)
  108.         {
  109.             System.out.println("\nElement not found\n");
  110.         }
  111.     }
  112.     /* Function myhash */
  113.     private int myhash(Integer x )
  114.     {
  115.         int hashVal = x.hashCode( );
  116.         hashVal %= table.length;
  117.         if (hashVal < 0)
  118.             hashVal += table.length;
  119.         return hashVal;
  120.     }
  121.     /* Function to generate next prime number >= n */
  122.     private static int nextPrime( int n )
  123.     {
  124.         if (n % 2 == 0)
  125.             n++;
  126.         for ( ; !isPrime( n ); n += 2);
  127.  
  128.         return n;
  129.     }
  130.     /* Function to check if given number is prime */
  131.     private static boolean isPrime( int n )
  132.     {
  133.         if (n == 2 || n == 3)
  134.             return true;
  135.         if (n == 1 || n % 2 == 0)
  136.             return false;
  137.         for (int i = 3; i * i <= n; i += 2)
  138.             if (n % i == 0)
  139.                 return false;
  140.         return true;
  141.     }
  142.     /* Function to print hash table */
  143.     public void printHashTable ()
  144.     {
  145.         System.out.println();
  146.         for (int i = 0; i < table.length; i++)
  147.         {
  148.             System.out.print ("Bucket " + i + ":  ");            
  149.  
  150.             DLLNode start = table[i];
  151.             while(start != null)
  152.             {
  153.                 System.out.print(start.data +" ");
  154.                 start = start.next;
  155.             }
  156.             System.out.println();
  157.         }
  158.     }    
  159.  
  160. }
  161.  
  162. /* Class HashTableChainingDoublyLinkedListTest */
  163. public class HashTableChainingDoublyLinkedListTest
  164. { 
  165.     public static void main(String[] args) 
  166.     {
  167.         Scanner scan = new Scanner(System.in);
  168.         System.out.println("Hash Table Test\n\n");
  169.         System.out.println("Enter size");
  170.         /* Make object of HashTableChainingDoublyLinkedList */
  171.         HashTableChainingDoublyLinkedList htcdll = new HashTableChainingDoublyLinkedList(scan.nextInt() );
  172.  
  173.         char ch;
  174.         /*  Perform HashTableChainingDoublyLinkedList operations  */
  175.         do    
  176.         {
  177.             System.out.println("\nHash Table Operations\n");
  178.             System.out.println("1. insert ");
  179.             System.out.println("2. remove");
  180.             System.out.println("3. clear");
  181.             System.out.println("4. size"); 
  182.             System.out.println("5. check empty");
  183.  
  184.             int choice = scan.nextInt();             
  185.             switch (choice)
  186.             {
  187.             case 1 : 
  188.                 System.out.println("Enter integer element to insert");
  189.                 htcdll.insert( scan.nextInt() ); 
  190.                 break;                          
  191.             case 2 :                  
  192.                 System.out.println("Enter integer element to delete");
  193.                 htcdll.remove( scan.nextInt() ); 
  194.                 break; 
  195.             case 3 : 
  196.                 htcdll.makeEmpty();
  197.                 System.out.println("Hash Table Cleared\n");
  198.                 break;
  199.             case 4 : 
  200.                 System.out.println("Size = "+ htcdll.getSize() );
  201.                 break; 
  202.             case 5 : 
  203.                 System.out.println("Empty status = "+ htcdll.isEmpty() );
  204.                 break;        
  205.             default : 
  206.                 System.out.println("Wrong Entry \n ");
  207.                 break;   
  208.             } 
  209.             /* Display hash table */ 
  210.             htcdll.printHashTable();  
  211.  
  212.             System.out.println("\nDo you want to continue (Type y or n) \n");
  213.             ch = scan.next().charAt(0);                        
  214.         } while (ch == 'Y'|| ch == 'y');  
  215.     }
  216. }

advertisement
Hash Table Test
 
 
Enter size
5
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
1
Enter integer element to insert
99
 
Bucket 0:
Bucket 1:
Bucket 2:
Bucket 3:
Bucket 4:  99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
1
Enter integer element to insert
23
 
Bucket 0:
Bucket 1:
Bucket 2:
Bucket 3:  23
Bucket 4:  99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
1
Enter integer element to insert
36
 
Bucket 0:
Bucket 1:  36
Bucket 2:
Bucket 3:  23
Bucket 4:  99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
1
Enter integer element to insert
47
 
Bucket 0:
Bucket 1:  36
Bucket 2:  47
Bucket 3:  23
Bucket 4:  99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
1
Enter integer element to insert
80
 
Bucket 0:  80
Bucket 1:  36
Bucket 2:  47
Bucket 3:  23
Bucket 4:  99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
1
Enter integer element to insert
37
 
Bucket 0:  80
Bucket 1:  36
Bucket 2:  37 47
Bucket 3:  23
Bucket 4:  99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
1
Enter integer element to insert
92
 
Bucket 0:  80
Bucket 1:  36
Bucket 2:  92 37 47
Bucket 3:  23
Bucket 4:  99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
1
Enter integer element to insert
49
 
Bucket 0:  80
Bucket 1:  36
Bucket 2:  92 37 47
Bucket 3:  23
Bucket 4:  49 99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
2
Enter integer element to delete
37
 
Bucket 0:  80
Bucket 1:  36
Bucket 2:  92 47
Bucket 3:  23
Bucket 4:  49 99
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
2
Enter integer element to delete
99
 
Bucket 0:  80
Bucket 1:  36
Bucket 2:  92 47
Bucket 3:  23
Bucket 4:  49
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
2
Enter integer element to delete
80
 
Bucket 0:
Bucket 1:  36
Bucket 2:  92 47
Bucket 3:  23
Bucket 4:  49
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
3
Hash Table Cleared
 
 
Bucket 0:
Bucket 1:
Bucket 2:
Bucket 3:
Bucket 4:
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. clear
4. size
5. check empty
5
Empty status = true
 
Bucket 0:
Bucket 1:
Bucket 2:
Bucket 3:
Bucket 4:
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.