0% found this document useful (0 votes)
8 views

Hashtable

Uploaded by

vovico3997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Hashtable

Uploaded by

vovico3997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package Hash_Assignment;

public class HashTable {


private int table[];
private int size;

public HashTable(){
table=new int[10];
size=0;
}

int hash(int value){


return value%10;
}

void insert_val(int value){


if(size==table.length)
System.out.println("Hash table full");

int index = hash(value);

if(table[index]==0){ //if slot empty, insert key


table[index]=value;
size++;
}
else{
for(int i=1;i<table.length;i++){ //slot not empty, use linear probing
int lpIndex=(index+i)%table.length;
if(table[lpIndex]==0){
table[lpIndex]=value;
size++;
break;
}
}
}
}

boolean search_val(int value){


int index=hash(value);

if(table[index]==value)
return true;

for(int i=1; i<table.length;i++){


int lpIndex=(index+i)%table.length;
if(table[lpIndex]==value)
return true;
}
return false;
}

void print_Hash_table(){
System.out.println("Hash Table :");
for(int i=0;i<table.length;i++)
{
System.out.print(table[i]+" ");
}
System.out.println();
}
}

You might also like