链地址法

package d17链地址法;
//员工信息类
public class Info {
    private String key;
    private String name;

    public Info(String key ,String name){
        this.key=key;
        this.name=name;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
package d17链地址法;
//链接点,相当于车厢
public class Node {
    //数据域
    public Info info;
    //节点域(指针域)
    public Node next;

    public Node(Info info){
        this.info=info;
    }

}
package d17链地址法;


//链表,相当于火车
public class LinkList {
    //车头
    private Node first;
    //默认构造方法 
    public LinkList(){
        first=null;
    }
    //插入一个节点,在头结点后进行插入
    public void insertFirst (Info info){
        Node node = new Node(info);
        node.next=first;
        first=node;//注意先后步骤  //倒着插入
    }
    //删除一个结点,在头结点后进行删除
    public Node deleteFirst(){
        Node tmp = first;
        first=tmp.next;
        return tmp;
    }
    //查找方法
    public Node find (String key){
        Node current = first;
        while(!key.equals(current.info.getKey())){
            if(current.next==null){
                return null;
            }
            current = current.next;
        }
        return current;
    }
    //删除方法,根据数据域进行删除
    public Node delete(String key){
        Node current = first;
        Node previous = first;
        while(!key.equals(current.info.getKey())){
            if(current.next==null){
                return null;
            }
            previous = current;
            current = current.next;
        }
        if(current==first){
        first = first.next;
        }else{
        previous .next=current.next;
        }
        return current;
    }
}
package d17链地址法;

import java.math.BigInteger;

public class HashTable {
    private LinkList [] arr;
    //默认构造方法
    public HashTable(){
        arr= new LinkList[100];
    }
    //指定数组初始化大小
    public HashTable(int maxSize){
        arr = new LinkList[maxSize];
    }
    //插入数据
    public void insert( Info info){
        //获取关键字
        String key = info.getKey();
        //关键字所自定的哈希数
        int hasVal = hashCode(key);
        if(arr[hasVal]==null){
            arr[hasVal]=new LinkList();
        }
        arr[hasVal].insertFirst(info);

    }
    private int hashCode(String key) {
        BigInteger hashVal = new BigInteger("0");
        BigInteger pow27 = new BigInteger("1");
        for(int i = key.length()-1;i>=0;i--){
            int letter = key.charAt(i)-96;
            BigInteger letterB = new BigInteger(String.valueOf(letter));
            hashVal=hashVal.add(letterB.multiply(pow27));
            pow27 = pow27.multiply(new BigInteger(String.valueOf(27)));
        }
        return hashVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();
    }
    //查找数据
    public Info find (String key ){
        int hashVal = hashCode(key);
        return arr[hashVal].find(key).info;
    }
    //删除数据
    public Info delete(String key){
        int hashVal=hashCode(key);
        return arr[hashVal].delete(key).info;
    }

}
package d17链地址法;

public class Test {

    public static void main(String[] args) {
        HashTable ht = new HashTable();
        ht.insert(new Info("a","张三"));
        ht.insert(new Info("ct","李四"));

        System.out.println(ht.find("a").getName());
        System.out.println(ht.find("ct").getName());

         System.out.println(ht.delete("a").getName());
         System.out.println(ht.find("a").getName());
    }

}

引用块内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值