哈希表-链地址法

本文详细探讨了哈希表的链地址法,通过将底层数组元素替换为链表,实现高效的数据插入操作。这种方法使得哈希冲突的解决变得简单,每个哈希值对应一个链表,相同哈希值的数据被链接在一起。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链地址法其实就是将实现哈希表的底层数组的类型变成链表,每次插入数据插入到链表中即可

这里写图片描述

package map;

public class Node
{
    private int data ;
    public Node next ;

    public Node ( int data )
    {
        this.data = data ;
    }

    public int getData ()
    {
        return data;
    }

    public void setData ( int data )
    {
        this.data = data;
    }


}
---------------
package map;

public class LinkList
{
    private Node first ;//首节点
    private Node current ;//用来记录当前节点
    private Node newNode ;

    //插入数据
    public void insert ( int i )
    {
        newNode = new Node ( i ) ;

        if ( first == null )
        {
            first = newNode ;
            return ;
        }

        newNode.next = first ;
        first = newNode ;

    }

    public void delete ( int value )
    {
        current = first ;
        Node parent = first ;
        while ( current != null )
        {
            if ( current.getData () == value )
            {
                parent.next = current.next ;
                return ;
            }
            parent = current ;
            current = current.next ;
        }
    }

    public Node find ( int value )
    {
        current  = first ;

        while ( current != null && current.getData () != value )
        {
            current = current.next ;
        }

        //跳出循环说明找到或者为空
        if ( current == null )
        {
            System.out.println ( "null" );
            return null ;
        }
        else
        {
            return current ;
        }
    }

    public void display ()
    {
        current = first ;
        while ( current != null )
        {
            System.out.print ( current.getData () + " " );
            current = current.next ;
        }
        System.out.println (  );
    }
}
-----------------------
package map;

public class LinkHash
{
    private LinkList [] arr ;
    private int size  ;
    private Node newNode ;

    public LinkHash ( int size )
    {
        this.size = size ;
        arr = new LinkList [size] ;
    }

    public int Hash ( int data )
    {
        return data % arr.length ;
    }

    //插入
    public void insert ( int data )
    {
        int index = Hash ( data ) ;//计算索引下标

        //如果为空,就new一个list进去
        if ( arr [index] == null )
        {

            LinkList l = new LinkList () ;
            l.insert ( data );
            arr [index] = l ;
        }
        else
        {//不为空直接入
            arr [index].insert ( data );
        }
    }

    public Node find ( int data )
    {
        int index = Hash ( data ) ;

        if ( arr [index] == null )
        {
            return null ;
        }
        else 
        {
            Node n = arr [index].find ( data ) ;

            if ( n == null )
            {
                return null ;
            }
            else
            {
                return n ;
            }
        }

    }

    public void delete ( int data )
    {
        int index = Hash ( data ) ;

        arr [index].delete ( data );
    }

    public void display ()
    {
        for ( int i = 0 ; i < arr.length ; ++ i )
        {
            if ( arr [i] != null )
            arr [i].display ();
            else
                System.out.println ();
        }
        System.out.println ( "-------------" );
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值