Tag Archives: Hash Function

Java Data Structure Implementation : Hashtable using Link List

This program demonstrates the hash table implementation using link list in Java. The Insert, Delete, Find and Print all Bucket Data Operation has been implemented.

The Hashtable implementation looks like :

Hashtable

This implementation is implemented using the Link List so you need to download my Doubly link list code into your package before running this code. You can download the Doubly link list code from my previous blog

Find the Java Code below :

import hclinfosys.ds.linklist.DoublyLinkList;

import java.util.Scanner;

/*
* This class demonstrates Hashtable Implementation in java with Add/Delete/Search Operations.
* @author : Madan Gopal Singh
* @version : 1.0
* @date : 31st March, 2013
*/
public class HashTable {
//Declaring Bucket Size
public static int HASH_TABLE_SIZE = 5;
//Declaring Buckets
public static HashTable[] buckets = new HashTable[HASH_TABLE_SIZE];

int id;
DoublyLinkList list;

/**
* This method getBucketHashFunction is used to return the position of element of which the element will be inserted into the bucket
* in hash table
* @param element
* @return int
*/
public static int getBucketHashFunction(int element){
return (element%HASH_TABLE_SIZE);
}

/**
* This method insertIntoBucket is used to insert element into the hashtable
* @param element
* @return void
*/
public static void insertIntoBucket(int element){
int bucket;
bucket = getBucketHashFunction(element);
if(buckets[bucket].list==null){
buckets[bucket].list = new DoublyLinkList(element);
}else{
DoublyLinkList.addNewNodeAtLast(buckets[bucket].list, new DoublyLinkList(element));
}
}

/**
* This method removeFromBucket is used to remove element from the hashtable
* @param element
* @return void
*/
public static void removeFromBucket(int element){
int bucket;
bucket = getBucketHashFunction(element);
if(buckets[bucket].list!=null){
if(((DoublyLinkList)buckets[bucket].list).next ==null){
buckets[bucket].list = null;
}else{
buckets[bucket].list = DoublyLinkList.removeNodeFromSpecificPosition(buckets[bucket].list, element);
}
}else{
System.out.println("Element is not found in Hashtable");
}
}

/**
* This method findInBuckets is used to find element in the hashtable
* @param element
* @return int
*/
public static int findInBuckets(int element){
int bucket;
bucket = getBucketHashFunction(element);
if(buckets[bucket].list!=null){
DoublyLinkList.traverseLinkListSpecificNode(buckets[bucket].list, element);
System.out.println("Found element in bucket "+bucket);
}else{
System.out.println("Element is not in Hashtable");
}
return 0;
}

/**
* This method findInBuckets is used to find element in the hashtable
* @param element
* @return int
*/
public static void printAllElements(){
for(int i = 0; i<buckets.length;i++){
if(buckets[i].list!=null){
System.out.println("Bucket "+ i + " elements : ");
DoublyLinkList.traverseLinkList(buckets[i].list);
}else{
System.out.println("Bucket "+ i + " has no element yet");
}
}
}
/**
* This method main method to perform the Doubly Link List Operations.
* @param args command line argument list
* @return void.
*/
public static void main(String[] args){
int userOperation = 0, element=-1, elementPostion= -1;
DoublyLinkList startNode = null;
int elementToBeInserted;
Scanner sc = new Scanner(System.in);

//Creating Blank Buckets
for(int i = 0; i<buckets.length;i++){
buckets[i] = new HashTable();
}

do{
String menuString = "\tHash Table \nWhat operation you want to perform?"+
"\n 1. Insert Element into Bucket"+
"\n 2. Remove Element from Bucket"+
"\n 3. Find Element in Bucket"+
"\n 4. Print All Elements of Hashtable"+
"\n -1. Exit from Menu"+
"\n Please enter Operation Number : ";
System.out.println(menuString);
userOperation = sc.nextInt();

switch(userOperation){
case 1:
System.out.println("Plese enter Element to Insert: ");
elementToBeInserted = sc.nextInt();
HashTable.insertIntoBucket(elementToBeInserted);
break;
case 2:
System.out.println("Plese enter Element to Remove: ");
elementToBeInserted = sc.nextInt();
HashTable.removeFromBucket(elementToBeInserted);
break;
case 3:
System.out.println("Plese enter Element to Find: ");
elementToBeInserted = sc.nextInt();
HashTable.findInBuckets(elementToBeInserted);
break;
case 4:
HashTable.printAllElements();
break;
case -1:
System.out.println("Exit Application");
System.exit(0);
default : System.out.println("Exit from System");
}
}while(userOperation!=-1);
}
}

Any suggestion is invited.

Advertisements