天天看點

數組+連結清單 實作簡單的散列

package com.cai.math;

/**
 * 哈希散列 之:數組+連結清單(這裡将要實作的部分)
 * 哈希散列是一種資料結構
 */
public class HashTabLinked {
    public static void main(String[] args) {
       // EmpLinked linked = new EmpLinked();
        ArrayLinked.add(new Emp(1,"路飛"));
        ArrayLinked.add(new Emp(2,"薩博"));
        ArrayLinked.add(new Emp(3,"索隆"));
        ArrayLinked.add(new Emp(7,"娜美"));
        ArrayLinked.add(new Emp(8,"山治"));
        ArrayLinked.add(new Emp(9,"喬巴"));
        ArrayLinked.findAll();
    }
}

/**
 * 數組連結清單:
 * 用取模的方法,存放資料到數組對應的位置
 */
class ArrayLinked{
    private final  static int SIZE = 7; //設定數組的長度(預設為7)
    private final  static  EmpLinked[] ARRAY_EMP_LINKED= new EmpLinked[SIZE]; //連結清單的數組

    /**
     * 根據取模方法找出 no存放對應數組的位置
     * @param no
     * @return 數組下角标
     */
    public static int getIndex(int no){
        return no%SIZE;
    }
    //實作增,查
    public static void add(Emp emp){
        //1.查找到要存入數組對應的位置
        int index = getIndex(emp.getNo());
        //2.如果數組目前位置為空,建立目前位置新的連結清單;如果已有,直接在目前連結清單上新增
        EmpLinked linked = ARRAY_EMP_LINKED[index];
        if(linked == null){
            linked = new EmpLinked();
            ARRAY_EMP_LINKED[index] = linked;
            linked.setHead(emp);
        }else{
            linked.add(emp);
        }
    }
    public static void findAll(){
        int count = 0;
        for (EmpLinked empLinked: ARRAY_EMP_LINKED) {
            System.out.println("第"+(++count)+"條鍊路");
            if(empLinked !=null){
                empLinked.findAll();
            }else{
                System.out.println("目前沒有數組");
            }

        }
    }
}
/**
 * 連結清單
 */
class EmpLinked{
    private Emp head;//連結清單的頭指針
    //實作增查
    public void add(Emp emp){
        if(head==null){
            head = emp;
            return;
        }
        Emp temp = head;
        while (true){
            if((temp.getNext())==null){
                temp.setNext(emp);
                break;
            }
            temp = temp.getNext();
        }
    }
    public void findAll(){
        if(head==null){
            System.out.println("沒有資料");
            return;
        }
        Emp temp = head;
        while (true){
            System.out.println(temp.toString());
            if((temp.getNext())==null){
                break;
            }
            temp = temp.getNext();
        }
    }
    public Emp getHead() {
        return head;
    }
    public void setHead(Emp head) {
        this.head = head;
    }
}

/**
 * 實體
 */
class Emp{
    private int no;
    private String name;
    private Emp next;

    public Emp(int no,String name){
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public Emp getNext() {
        return next;
    }

    public void setNo(int no) {
        this.no = no;
    }

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

    public void setNext(Emp next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}
列印:
      

第1條鍊路

Emp{no=7, name='娜美'}

第2條鍊路

Emp{no=1, name='路飛'}

Emp{no=8, name='山治'}

第3條鍊路

Emp{no=2, name='薩博'}

Emp{no=9, name='喬巴'}

第4條鍊路

Emp{no=3, name='索隆'}

第5條鍊路

目前沒有數組

第6條鍊路

第7條鍊路