代码地址https://gitee.com/ALi_L/javaDataStructurs.githttps://gitee.com/ALi_L/javaDataStructurs.git目录

思路

图解如下

各个代码

整体代码


思路

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

用数组+链表实现哈希表相关推荐

  1. codewars打怪日记 Greed is Good JavaScript中数组用法和 哈希表的使用

    codewars是一个在线编程网站,其奖励机制像打怪升级.你不能查看高于你级别的问题的答案.除非自己通过提交测试.通过提交之后可以看到各种解法排行榜 .通过对比自己解法和排行榜对比,可以找到差距,提高 ...

  2. 牛客网Java刷题知识点之数组、链表、哈希表、 红黑二叉树

    不多说,直接上干货! 首先来说一个非常形象的例子,来说明下数组和链表. 上体育课的时候,老师说:你们站一队,每个人记住自己是第几个,我喊到几,那个人就举手,这就是数组. 老师说,你们每个人记住自己前面 ...

  3. C++:数组、链表与哈希表

    文章目录 数组和链表 数组 什么是数组? 访问数组元素 可变长的动态数组:vector Vector基本用法 链表 什么是链表? 链表的操作 双向链表(list) list的成员函数 哈希表 什么是哈 ...

  4. java 静态 二维数组 转化hashmap_将一个二维数组转换为 hashmap 哈希表

    /** * 将一个二维数组转换为 hashmap * * 如果省略 $val 参数,则转换结果每一项为包含该项所有数据的数组. * * @param array $arr * @param strin ...

  5. leetcode - 141. 环形链表(哈希表)

    给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 1: 输入: ...

  6. 【LeetCode刷题记录】LeetCode经典题目数组求和及哈希表的使用!

    点上方蓝字计算机视觉联盟获取更多干货 在右上方 ··· 设为星标 ★,与你不见不散 LeetCode题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整 ...

  7. Linux哈希表数组,开地址哈希表(Hash Table)的接口定义与实现分析

    开地址哈希函数的接口定义 基本的操作包括:初始化开地址哈希表.销毁开地址哈希表.插入元素.删除元素.查找元素.获取元素个数. 各种操作的定义如下: ohtbl_init int ohtbl_init ...

  8. Python数据结构与算法笔记(七):数据结构——队列,链表和哈希表

    队列 看成人在排队. 在出队到最后一个元素时,再想入队,使用列表可以在后面append,但是前面仍然占据着一部分内存,无法处理.想个办法让其收尾连成一个圈. 队列的实现方式:环形队列 判定一个队列是否 ...

  9. 链表实现哈希表以及哈希表的应用

    ''' 哈希表 字典和集合一个通过哈希函数来计算数据存储位置的数据结构,通常支持如下操作: insert:插入键值对 get :如果存在键为key的键值对则返回其value,否则返回空值 delete ...

最新文章

  1. swift x输入流_SwiftUI 探索 - 状态和数据流
  2. 在内部局域网(无外网)使用阿里云短信
  3. PPT 下载 | 神策数据杜明翰:打造趁手、好用的标签用户画像系统
  4. EnvironmentError: mysql_config not found
  5. POJ3233 Matrix Power Series
  6. win8计算机配置怎么看,win8怎么看电脑配置?win8电脑配置的查看方法
  7. js中将html文档写入静态界面当中
  8. 在moss上自己总结了点小经验。。高手可以飘过
  9. 微信小程序抖音实战-支持手机播放小视频
  10. (转)比特币有了定价模型?过去四年94%的价格波动可由此解释
  11. teechart的addarray_TeeChart经验总结 6.Series之1:Line
  12. 设计网站中的精品,你可能需要它--第二期
  13. 网络安全系列之培训笔记整理
  14. 面对初学者的CAN总线入门教程(二)_CAN协议基本概念及其规格标准
  15. 三相 AC-DC 变换电路(B 题)-- 2021 年全国大学生电子设计竞赛
  16. 计算机技术服务业成本核算,如何进行服务行业的成本核算
  17. YUVPlayer: 基于Android平台的YUV视频原始数据播放器
  18. 【阿里巴巴】学习Java在面试过程中跳槽成功的心得总结
  19. Delphi Web前端开发教程(9):基于TMS WEB Core框架
  20. [Vue warn]: Failed to resolve component:XXX

热门文章

  1. NLP文档挖宝(1)——tokenizer的诞生
  2. mahout探索之旅---频繁模式挖掘算法与理解
  3. 与android虚拟机传输文件,android 在本机和虚拟机设备之间复制文件
  4. 报错:RuntimeError: view size is not compatible with input tensor‘s size and stride (at least one dimen
  5. 12个成功案例,告诉你提升客户体验新方法!
  6. 1024,程序员“赚钱”秘籍倾囊而赠!
  7. 利用Python对淘宝用户行为进行分析
  8. TCP/IP协议分析实验
  9. 在Linux(fedora 20)上解压缩rar文件
  10. 压力测试-Jmeter脚本录制方案