java 链表哈希表

package com.guge.test;

import java.util.Random;

/**
 * Created by guugangzhu on 2017/6/1.
 */
public class HashLink {


    public static void main(String[] args) {
        HashLink hashLink=new HashLink(59);
        Random random=new Random();
        for (int i = 0; i <100 ; i++) {
            Link link=new Link(random.nextInt(500));
            hashLink.insert(link);
        }
        hashLink.displayTable();

    }

    private SortedList[] hashArray;
    private int arraySize;

    public HashLink(int size){
        arraySize=size;
        hashArray=new SortedList[arraySize];

        for (int i = 0; i < arraySize; i++) {
            hashArray[i]=new SortedList();
        }
    }

    public void displayTable(){
        for (int i = 0; i < arraySize; i++) {
            System.out.print(i+" ");
            hashArray[i].displayList();;
        }
    }

    public int hashFunc(int key){
        return key%arraySize;
    }

    public void insert(Link theLink){
        int hashVal=hashFunc(theLink.getKey());
        hashArray[hashVal].insert(theLink);
    }

    public void delete(int key){
        int hashVal=hashFunc(key);
        hashArray[hashVal].delete(key);
    }

    public Link find(int key){
        int hashVal=hashFunc(key);
        return hashArray[hashVal].find(key);
    }

}
class Link{
    private int iData;
    public Link next;

    public Link(int it){
        iData=it;
    }

    public int getKey(){
        return iData;
    }

    public void displayLink(){
        System.out.print(iData+" ");
    }
}

class SortedList{
    private Link first;

    public SortedList(){
        first=null;
    }

    public void insert(Link theLink){
        int key=theLink.getKey();
        Link previous=null;
        Link current=first;

        while (current!=null&&key<current.getKey()){
            previous=current;
            current=current.next;
        }

        if(previous==null)
            first=theLink;
        else
            previous.next=theLink;
        theLink.next=current;
    }

    public void delete(int key){
        Link previous=null;
        Link current=first;

        while (current!=null&&key!=current.getKey()){
            previous=current;
            current=current.next;
        }

        if(previous==null&&first!=null)
            first=first.next;

        if(previous!=null&&current!=null){
            previous.next=current.next;
        }
    }

    public Link find(int key){
        Link current=first;

        while (current!=null&&key<current.getKey()){
            if(key==current.getKey()){
                return current;
            }
            current=current.next;
        }
        return null;
    }

    public void displayList(){
        System.out.println("List(first-->last)");
        Link current=first;

        while (current!=null){
            current.displayLink();
            current=current.next;
        }
        System.out.println("");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值