(第16讲)哈希表的链地址法

开放地址法中,通过在哈希表中在寻找一个空位解决冲突问题。这种方法是每个哈希表的单元存放一个数据项。此方法的查找和插入都是随着L剧烈增长的。

链地址法中,某个数据项的关键字还是照样映射到哈希表的某个单元,但数据项本身插入都这个单元中的链表;其他同样映射到这个单元的数据项,则直接加到链表中,不需要再原始的数组中寻找空位。每个哈希表的单元存放一个或多个数据项;

并且链地址法的查找和插入数据项的时间都是随着装填因子L线性增长的(装填因子=数据项数/数组容量)

/**
 * 哈希表的链地址法
 */
package com.eleven;

public class HashLinkApp {

    public static void main(String[] args) {
        int size = 20;
        HashLink hashtable = new HashLink(size);
        int[] arr = {143,1160,240,1004,1485,1585,87,1407,309,590,872,1073,594,954,335,1216,1057,1357,938,1818};
        for(int i=0;i<arr.length;i++){
            Data4 data = new Data4(arr[i]);
            hashtable.insert(data);
        }
         hashtable.display();
         //int findkey = 1817;
         int findkey = 1818;//设置要查找的关键值
         Node finddata = hashtable.find(findkey);
         if(finddata!=null)
             System.out.println("找到"+findkey);
         else
             System.out.println("没找到"+findkey);
        
         //删除操作
        int deletekey = 1357;
        //  int deletekey = 1356;
         Node deletedata = hashtable.delete(deletekey);
         if(deletedata!=null)
             System.out.println("已经删除"+deletekey);
         else
             System.out.println("该哈希表中不含"+deletekey);
         hashtable.display();
    }
 
}
//结点中的信息类
class Data4{
    private int data;
    public Data4(int data){
        this.data = data;
    }
    public int getData(){
        return data;
    }
}
//结点类
class Node{
    public  Data4 data;
    public Node next;
    public Node(Data4 data,Node next)
    {
        this.data = data;
        this.next = next;
    }
    public Node(Data4 data){
        this(data,null);
    }
    public Node(){
        this(null,null);
    }
}
//链表类(带头结点的有序链表类)即为数据类
class LinkListed{
    private Node head;
    public LinkListed(){
        head = new Node();
    }
    //增
    public void insert(Data4 value){
        Node newnode = new Node(value);
        Node node = head.next;
        Node pre = head;
        while(node!=null && value.getData()>node.data.getData()){
            pre = node;//pre是newnode的前驱
            node = node.next;//node是newnode的后继
        }
        pre.next = newnode;
        newnode.next = node;
    }
    //删
    public Node delete(Data4 value){
        Node node = head.next;
        Node pre = head;
         while(node!=null &&  node.data.getData()!= value.getData() ){
            pre = node;
            node = node.next;
        }
         if(node==null)
             return null;
        pre.next = node.next;
        return node;
    }
    //查
    public Node find(Data4 value){
        Node node = head.next;
        Node pre = head;
        //while(node!=null && !node.data.equals(value)){
        while(node!=null &&  node.data.getData()!= value.getData() ){
            pre = node;
            node = node.next;
        }
        return node;
    }
    //遍历
    public void display(){
        System.out.print("List(head--rear)");
        Node node = head.next;
        while(node!=null){
            System.out.print(node.data.getData()+" ");
            node = node.next;
        }
        System.out.println();
    }
}
//哈希表类(基于链表的)
class HashLink{
    private LinkListed[] hashArr;
    private int arrlen;
    public HashLink(int size){
        arrlen = size;
        hashArr = new LinkListed[arrlen];
        for(int i=0;i<hashArr.length;i++){
            hashArr[i] = new LinkListed();
        }
    }
    //哈希函数
    public int HashFunc(int key){
        return key % arrlen;
    }
    //增
    public void insert(Data4 value){
        int data = value.getData();
        int index = HashFunc(data);//得到哈希值,即数组下标
        //Node newnode = new Node(value);//得到将加入到链表中的结点对象
        hashArr[index].insert(value);
    }
    //删
    public Node delete(int value){
        Data4 data1 = new Data4(value);
        int index = HashFunc(value);//得到哈希值,即数组下标
        Node deletenode = hashArr[index].delete(data1);
        return deletenode;
    }
    //查
    public Node find(int value){
        Data4 data1 = new Data4(value);
        
        int index = HashFunc(value);
        Node findnode = hashArr[index].find(data1);
        return findnode;
    }
    //遍历
    public void display(){
        for(int i=0;i<hashArr.length;i++){
            System.out.print(i+".");
            hashArr[i].display();
        }
    }
}

结果是:

0.List(head--rear)240 1160
1.List(head--rear)
2.List(head--rear)
3.List(head--rear)143
4.List(head--rear)1004
5.List(head--rear)1485 1585
6.List(head--rear)
7.List(head--rear)87 1407
8.List(head--rear)
9.List(head--rear)309
10.List(head--rear)590
11.List(head--rear)
12.List(head--rear)872
13.List(head--rear)1073
14.List(head--rear)594 954
15.List(head--rear)335
16.List(head--rear)1216
17.List(head--rear)1057 1357
18.List(head--rear)938 1818
19.List(head--rear)
找到1818
已经删除1357
0.List(head--rear)240 1160
1.List(head--rear)
2.List(head--rear)
3.List(head--rear)143
4.List(head--rear)1004
5.List(head--rear)1485 1585
6.List(head--rear)
7.List(head--rear)87 1407
8.List(head--rear)
9.List(head--rear)309
10.List(head--rear)590
11.List(head--rear)
12.List(head--rear)872
13.List(head--rear)1073
14.List(head--rear)594 954
15.List(head--rear)335
16.List(head--rear)1216
17.List(head--rear)1057
18.List(head--rear)938 1818
19.List(head--rear)


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值