Thursday, 2 May 2013

Hashing


class Node{
int data;
Node next;

Node(int d){
data=d;

}

}
public class HTable {
Node [] Table;
int size;
HTable(int s){
size=s;
Table =new Node[size];

}
public void insert(int key){
Node newNode= new Node(key);
int hash =key%size;

if(Table[hash]==null)
Table[hash] = newNode;
else{
Node HashRef=Table[hash];
newNode.next =HashRef;
Table[hash] =newNode;


}
}
public void printhashtable(){
Node href = null;
for(int i=0; i<size; i++){
href = Table[i];
System.out.print("  " + href.data);
href = href.next;

}
System.out.println(" ");

}

}

==================================================



public class HDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
         HTable table1= new HTable(15);
         table1.insert(345);
         table1.insert(445);
         table1.insert(245);
         table1.insert(045);
         table1.insert(145);
         table1.printhashtable();
       
}

}



No comments:

Post a Comment