Skip to content

Instantly share code, notes, and snippets.

@Madhivarman
Last active September 5, 2017 14:19
Show Gist options
  • Save Madhivarman/299eeedf1252b0180a02702824d4f808 to your computer and use it in GitHub Desktop.
Save Madhivarman/299eeedf1252b0180a02702824d4f808 to your computer and use it in GitHub Desktop.
Hashing Table concept implemented in java
import java.io.*;
import java.util.Scanner;
class HashingTable{
static String userchoice;
static int capacity;
static int array[];
//constructor function called
public HashingTable(int capacity){
this.capacity = nextPrime(capacity);
array = new int[this.capacity];//array size
}
private static int nextPrime(int ele){
if(ele%2 == 0)
ele++;
//it starts to check from the value of the capacity
//to check if the number is prime
for(;!isPrime(ele);ele+=2);
return ele;
}
//to check whether the hashing table capacity is prime or not
//isPrime function is called
private static boolean isPrime(int ele){
//if prime return true
if(ele == 2 || ele == 3)
return true;
//if not prime return false
if(ele == 1 || ele%2 == 0)
return false;
//to check whether the capacity is prime
for(int i=3;i*i <= ele;i +=2)
if(ele%i == 0)
return false;
return true;
}
//insert function called
static void insert(int number){
//k mod n operation made here
array[number % capacity] = number;
}
//delete function called
static void delete(int number){
if(array[number % capacity] == number){
array[number % capacity] = 0;
}else{
System.out.println("Number is not found");
}
}
//search function is called
static void search(int number){
for(int i=0;i<capacity;i++){
//condition to check if the number is find
if(array[i] == number){
System.out.println("The number located at index:" + i + "th position");
}
}
}
static void clear(){
System.out.println("Clearing all Hash table values");
for(int i=0;i<capacity;++i){
array[i] = 0;
}
}
static void printHashTable(){
System.out.println("hash table contains:");
for(int i=0;i<capacity;i++){
System.out.print(array[i]+"->");
}
}
//main function
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the HashingTable:");
int size = sc.nextInt();
//object called
HashingTable obj = new HashingTable(size);
do{
Scanner string = new Scanner(System.in);
System.out.println("1.insert/n 2.Delete/n 3.Search/n 4.Clear");
System.out.print("Enter your choice:");
int choice = sc.nextInt();
switch (choice) {
case 1 :
System.out.println("Enter the number you want to insert:");
int element = sc.nextInt();
insert(element);//insert function called
break;
case 2 :
System.out.println("Enter the number you want to delete:");
int element_two = sc.nextInt();
delete(element_two); //delete function called
break;
case 3 :
System.out.println("Enter the number you want to search and locate where it is:");
int element_three = sc.nextInt();
search(element_three); // search function called
break;
case 4 :
System.out.println("Do you want to clear your hasing table ?");
String clear = string.next();
if(clear.equals("y")){
clear();//clear function called
}
break;
}
obj.printHashTable();
//user choice
userchoice = string.next();
}while(userchoice.equals("y"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment