Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program to implement Hashing with Linear Probing
- import java.util.Scanner;
- class hashTable{
- private int size, elementCount;
- private int[] table;
- public Scanner sc = new Scanner(System.in);
- // initialize hash Table
- hashTable(){
- System.out.print("Enter the size of the Table : ");
- this.size = sc.nextInt();
- this.table = new int[size];
- for(int i=0; i < size; i++){
- this.table[i] = 0;
- }
- this.elementCount = 0;
- }
- // method that checks if the hash table is full or not
- boolean isFull(){
- if(this.elementCount == this.size){
- return true;
- }else{
- return false;
- }
- }
- // method that returns position for a given element
- int hashFunction(int element){
- return element % this.size;
- }
- // method that inserts element inside the hash table
- void insert(int element){
- int position;
- // checking if the this.table is full
- if(this.isFull()){
- System.out.println("Hash Table Full");
- return;
- }
- boolean isStored = false;
- position = this.hashFunction(element);
- // checking if the position is empty
- if(this.table[position] == 0){
- this.table[position] = element;
- System.out.println("Element " + element + " at position " + position);
- isStored = true;
- this.elementCount += 1;
- }
- // collision occured hence we do linear probing
- else{
- System.out.println("Collision has occured for element " + element + " at position " + position + " finding new Position.");
- while(this.table[position] != 0){
- position += 1;
- if(position >= this.size){
- position = 0;
- }
- }
- this.table[position] = element;
- isStored = true;
- this.elementCount += 1;
- }
- return;
- }
- // method that searches for an element in the table
- // returns position of element if found
- // else returns false
- int search(int element){
- boolean found = false;
- int position = this.hashFunction(element);
- if(this.table[position] == element){
- found = true;
- return position;
- }
- // if element is not found at position returned hash function
- // then first we search element from position+1 to end
- // if not found then we search element from position-1 to 0
- else{
- int temp = position - 1;
- // check if the element is stored between position+1 to size
- while(position < this.size){
- if(this.table[position] != element){
- position += 1;
- }else{
- return position;
- }
- }
- // now checking if the element is stored between position-1 to 0
- position = temp;
- while(position >= 0){
- if(this.table[position] != element){
- position -= 1;
- }else{
- return position;
- }
- }
- }
- if(!found){
- System.out.println("Element not found");
- return -1;
- }
- return -1;
- }
- // method to remove an element from the table
- void remove(int element){
- int position = this.search(element);
- if(position != -1){
- this.table[position] = 0;
- System.out.println("Element " + element + " is Deleted");
- this.elementCount -= 1;
- }else{
- System.out.println("Element is not present in the Hash Table");
- }
- return;
- }
- // method to display the hash table
- void display(){
- for (int i = 0; i < this.size; i++){
- System.out.println(i + " = " + this.table[i]);
- }
- System.out.println("The number of element is the Table are : " + this.elementCount);
- }
- };
- public class hash{
- public static void main(String arg[]){
- hashTable table1 = new hashTable();
- // storing elements in this.table
- table1.insert(12);
- table1.insert(26);
- table1.insert(31);
- table1.insert(17);
- table1.insert(90);
- table1.insert(28);
- table1.insert(88);
- table1.insert(40);
- table1.insert(77); // element that causes collision at position 0
- // displaying the Table
- table1.display();
- // printing position of elements
- System.out.println("The position of element 31 is : " + table1.search(31));
- System.out.println("The position of element 28 is : " + table1.search(28));
- System.out.println("The position of element 90 is : " + table1.search(90));
- System.out.println("The position of element 77 is : " + table1.search(77));
- System.out.println("The position of element 1 is : " + table1.search(1));
- table1.remove(90);
- table1.remove(12);
- table1.display();
- }
- }
Add Comment
Please, Sign In to add comment