public class LinearProbHashing {
int[] table;
int size;
LinearProbHashing(int s){
size = s;
table = new int[size];
}
int rehash(int key){
return ((key+1)%size);
}
public void insert(int key){
int hash = key%size;
int RH = 0;
if(table[hash] == 0){
table[hash] = key;
}else{
RH = rehash(key);
while(RH != 0){
RH = rehash(RH++);
table[RH] = key;
}
}
}
public void printhashtable(){
for(int i=0; i<size; i++){
System.out.print(" "+table[i]);
}
}
}
=============================================
public class HashDemo {
public static void main(String[] args) {
// HashTable table1 = new HashTable(10);
LinearProbHashing table1 = new LinearProbHashing(10);
table1.insert(345);
table1.insert(245);
table1.insert(344);
table1.insert(312);
table1.insert(335);
table1.printhashtable();
}
}
int[] table;
int size;
LinearProbHashing(int s){
size = s;
table = new int[size];
}
int rehash(int key){
return ((key+1)%size);
}
public void insert(int key){
int hash = key%size;
int RH = 0;
if(table[hash] == 0){
table[hash] = key;
}else{
RH = rehash(key);
while(RH != 0){
RH = rehash(RH++);
table[RH] = key;
}
}
}
public void printhashtable(){
for(int i=0; i<size; i++){
System.out.print(" "+table[i]);
}
}
}
=============================================
public class HashDemo {
public static void main(String[] args) {
// HashTable table1 = new HashTable(10);
LinearProbHashing table1 = new LinearProbHashing(10);
table1.insert(345);
table1.insert(245);
table1.insert(344);
table1.insert(312);
table1.insert(335);
table1.printhashtable();
}
}
No comments:
Post a Comment