链表+数组实现哈希表

想进阿里的小菜鸡 2021-11-21 15:37:41

原文地址

 思路

  1. 用链表来存储数据;Emp
  2. 用数组来存放一个一个的链表;EmpLinkedList
  3. 再用一个类来管理数组。HashTab

图解如下

 各个代码

1.节点Emp

class Emp {
    private int id;
    private String name;
    public Emp next;

    public Emp() {
    }

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

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "=>" +
                "id=" + id +
                ", name='" + name + '\''
                ;
    }
}

2.节点管理EmpLinkedList

//接点管理链表。用来管理节点
class EmpLinkedList {
    private Emp head;

    public EmpLinkedList() {

    }

    /**
     * 增加链表
     *
     * @param emp 新增加的元素
     */
    public void add(Emp emp) {
        if (this.head == null) {
            head = emp;
            return;
        } else {
            Emp temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = emp;
        }
    }

    //展示链表信息
    public void list() {
        if (this.head == null) {
            System.out.println("空");
            return;
        }
        Emp temp = this.head;
        while (temp != null) {
            System.out.print(temp);
            temp = temp.next;
        }
        System.out.println();


    }

    //
    public Emp findEmpById(int id) {
        if (head == null) {
            return null;
        }
        Emp temp = this.head;
        while (temp != null) {
            if (temp.getId() == id) {
                return temp;
            } else {
                temp = temp.next;
            }
        }
        return null;
    }


}

3.哈希表(用数组来管理链表)HashTab

/**
 * 哈希表,里面存储的是EmpLinkedList类型的链表,是用数组实现的。
 */
class HashTab {
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    public HashTab(int size) {
        this.empLinkedListArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            this.empLinkedListArray[i] = new EmpLinkedList();
        }
        this.size = size;
    }

    /**
     * 添加元素
     *
     * @param emp
     */
    public void add(Emp emp) {
        int id = emp.getId();
        int index = id % size;
        //添加
        this.empLinkedListArray[index].add(emp);
    }

    /**
     * 展示链表
     */
    public void list() {
        if (this.size == 0) {
            System.out.println("为空");
        } else {
            for (int i = 0; i < this.size; i++) {
                System.out.print("第" + i + "条链表为:");
                this.empLinkedListArray[i].list();
            }
        }
    }

    public void findEmpById(int id) {

        if (this.size == 0) {
            System.out.println("链表为空");
            return;
        }
        Emp res = new Emp();
        int index = id % size;
        res = this.empLinkedListArray[index].findEmpById(id);
        if (res == null) {
            System.out.println("没有找到id为"+id+"的元素");
        }else {
            System.out.println("在第"+index+"条表中找到该雇员,id为"+id);
        }
    }

}

 

 整体代码

package DataStructures.HashTable;

/**
 * @author :ALi
 * @date :Created in 2021/11/21 14:28
 * @description:手写哈希表
 * @modified By:
 * @version: $
 */
public class HashTable {
    public static void main(String[] args) {
        Emp emp1 = new Emp(1, "tom1");
        Emp emp2 = new Emp(2, "tom2");
        Emp emp3 = new Emp(7, "tom2");
        HashTab ht = new HashTab(6);
        ht.add(emp1);
        ht.add(emp2);
        ht.add(emp3);
        ht.list();
        ht.findEmpById(22);
    }


}

//节点链表,只用来存储节点元素
class Emp {
    private int id;
    private String name;
    public Emp next;

    public Emp() {
    }

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

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "=>" +
                "id=" + id +
                ", name='" + name + '\''
                ;
    }
}

//接点管理链表。用来管理节点
class EmpLinkedList {
    private Emp head;

    public EmpLinkedList() {

    }

    /**
     * 增加链表
     *
     * @param emp 新增加的元素
     */
    public void add(Emp emp) {
        if (this.head == null) {
            head = emp;
            return;
        } else {
            Emp temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = emp;
        }
    }

    //展示链表信息
    public void list() {
        if (this.head == null) {
            System.out.println("空");
            return;
        }
        Emp temp = this.head;
        while (temp != null) {
            System.out.print(temp);
            temp = temp.next;
        }
        System.out.println();


    }

    //
    public Emp findEmpById(int id) {
        if (head == null) {
            return null;
        }
        Emp temp = this.head;
        while (temp != null) {
            if (temp.getId() == id) {
                return temp;
            } else {
                temp = temp.next;
            }
        }
        return null;
    }


}

/**
 * 哈希表,里面存储的是EmpLinkedList类型的链表,是用数组实现的。
 */
class HashTab {
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    public HashTab(int size) {
        this.empLinkedListArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            this.empLinkedListArray[i] = new EmpLinkedList();
        }
        this.size = size;
    }

    /**
     * 添加元素
     *
     * @param emp
     */
    public void add(Emp emp) {
        int id = emp.getId();
        int index = id % size;
        //添加
        this.empLinkedListArray[index].add(emp);
    }

    /**
     * 展示链表
     */
    public void list() {
        if (this.size == 0) {
            System.out.println("为空");
        } else {
            for (int i = 0; i < this.size; i++) {
                System.out.print("第" + i + "条链表为:");
                this.empLinkedListArray[i].list();
            }
        }
    }

    public void findEmpById(int id) {

        if (this.size == 0) {
            System.out.println("链表为空");
            return;
        }
        Emp res = new Emp();
        int index = id % size;
        res = this.empLinkedListArray[index].findEmpById(id);
        if (res == null) {
            System.out.println("没有找到id为"+id+"的元素");
        }else {
            System.out.println("在第"+index+"条表中找到该雇员,id为"+id);
        }
    }

}

 

 

...全文
45 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,633

社区成员

发帖
与我相关
我的任务
社区描述
欢迎大家来到抱团内卷学习社区,在这里大家可以分享自己的学习笔记,求职心得,一起记录彼此的成长历程。社区群号:94108843,WX公众号:【兴趣使然的草帽路飞】
社区管理员
  • 路  飞
  • 一百个Chocolate
  • 灰小猿
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

最怕你一生碌碌无为,还安慰自己平凡可贵!

努力提高自己的知识储备,助力每一位冲刺大厂的小伙伴!

祝大家前程似锦,offer连连!

注意:每个月活跃积分最高的小伙伴,可以获得社区管理员权限哦!

试试用AI创作助手写篇文章吧