链表+数组实现哈希表

想进阿里的小菜鸡 想进阿里的小菜鸡 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

原文地址

 思路

  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);
        }
    }

}

 

 

...全文

相关推荐

详解散列表算法与其相关的C语言实现

主要介绍了详解散列表算法与其相关的C语言实现,平时经常出现于各大考试竞赛与程序员面试题目当中,需要的朋友可以参考下

weixin_38662327

c语言或c++课程设计之散列表哈希表

c语言或c++课程设计之散列表哈希表 c语言或c++课程设计之散列表哈希表 c语言或c++课程设计之散列表哈希表

feige0924

利用哈希表进行存储 针对一组数据进行初始化哈希表,可以进行显示哈希表,查找元素,插入元素,删除元素,退出程序操作

【问题描述】 利用哈希表进行存储。针对一组数据进行初始化哈希表,可以进行显示哈希表,查找元素,插入元素,删除元素,退出程序操作。 【任务要求】 (1)用户可以进行创建哈希表,显示哈希表,查找元素,插入元素,删除元素。 (2)设计思想:哈希函数用除留余数法构造,用线性探测再散列处理冲突。 (3)显示元素:显示已经创建的哈希表。 (4)查找元素:查找哈希表中的元素,分为查找成功和查找不成功。 (5)插入元素:在哈希表中,插入一个元素,分为插入成功和失败。 (6)删除元素:在已有的数据中,删除一个元素。 (7)退出系统:退出程序。 涉及的数据结构知识点和算法: ①哈希表(Hash table) ②哈希函数(Hash function) ③冲突处理方法(Collision resolution) ④除留余数法(Modulo hashing) ⑤线性探测再散列法(Linear probing) 概述: 哈希表(Hash table)是一种基于哈希函数(Hash function)实现的数据结构,用于实现关联数组或者映射等抽象数据类型。哈希表将元素的键(key)通过哈希函数转换为哈希值

小柒_02

哈希表(散列表)和哈希查找

哈希表(散列表)和哈希查找方法,解决冲突方法教程

lxd445062801

hashtable:C中的哈希表

哈希表 实现动态类型存储系统的哈希表 建造 您必须安装了 AutoTools。 然后运行autoreconf --install然后执行./configure ,然后执行make

邱笑晨
评论
收藏 已收藏
分享
返回顶部