ChainingHashtable

  1 public class ChainingHashtable {
  2 
  3     private static class Node{
  4         private int key;
  5         private String value;
  6         private Node next;
  7         
  8         public Node(int key,String value){
  9             this.key=key;
 10             this.value=value;
 11             this.next=null;
 12         }
 13         public int getKey(){
 14             return this.key;
 15         }
 16         public void display(){
 17             System.out.println("key: "+this.key+",value: "+this.value);
 18         }
 19     }
 20     private static class nodeList{
 21         private Node first;
 22         public nodeList(){
 23             this.first=null;
 24         }
 25         public void insert(Node n){
 26             Node current=this.first;
 27             Node previous=null;
 28             while(current!=null){
 29                 previous=current;
 30                 current=current.next;
 31             }
 32             if(previous!=null){
 33                 previous.next=n;
 34             }
 35             else{
 36                 this.first=n;
 37             }
 38             
 39         }
 40         public Node find(int key){
 41             Node current=this.first;
 42             while(current!=null && key!=current.getKey()){
 43                 current=current.next;
 44             }
 45             return current;
 46         }
 47         public void display() {
 48             Node current = this.first;
 49             while (current != null) {
 50                 current.display();
 51                 current = current.next;
 52             }
 53         }
 54     }
 55     
 56     
 57     private nodeList[] hashtable;
 58     private int tablesize;
 59     public ChainingHashtable(int size){
 60         this.tablesize=size;
 61         this.hashtable=new nodeList[tablesize];
 62         for (int i = 0; i < this.tablesize; i++) {
 63             this.hashtable[i] = new nodeList();
 64         }
 65     }
 66     
 67     public int keyFunction(int key){
 68         return key%tablesize;
 69     }
 70     
 71     public void insert(Node n){
 72         int key=keyFunction(n.getKey());
 73         this.hashtable[key].insert(n);
 74     }
 75     
 76     public Node find(int key){
 77         int k=keyFunction(key);
 78         return this.hashtable[k].find(key);
 79     }
 80     
 81     public void display() {
 82         for (int i = 0; i < this.tablesize; i++) {
 83             System.out.println("i: " + i);
 84             this.hashtable[i].display();
 85         }
 86     }
 87     
 88     public static void main(String[] args) {
 89         
 90         ChainingHashtable chainingHashtable=new ChainingHashtable(5);
 91         
 92         for (int i = 1; i < 12; i++) {
 93             Node n=new Node(i,"value"+i);
 94             chainingHashtable.insert(n);
 95         }
 96 
 97         chainingHashtable.display();
 98     }
 99 
100 }

 

posted on 2013-04-01 15:44  melotang  阅读(174)  评论(0)    收藏  举报