简单hashtab的实现

package com.atguigu.hashtable;import java.util.Scanner;/*** @author zhangtao* @date 2022/9/18 - 16:05*/
public class HashtableDemo {public static void main(String[] args) {HashTab hashTab = new HashTab(7);String key = "";Scanner scanner = new Scanner(System.in);while (true) {System.out.println("add: 添加员工");System.out.println("list:显示员工");System.out.println("find:查找员工");System.out.println("delete:删除员工");System.out.println("update:更新员工");System.out.println("exit:退出");System.out.print("请输入您的选择:");key = scanner.next();switch (key) {case "add":System.out.print("请输入id:");int id = scanner.nextInt();System.out.print("请输入名字:");String name = scanner.next();hashTab.add(new Emp(id, name));break;case "list":hashTab.list();break;case "find":System.out.print("请输入你要查找的员工id:");Emp empById = hashTab.findEmpById(scanner.nextInt());System.out.println("empById = " + empById);break;case "delete":System.out.print("请输入你要删除的员工id:");hashTab.delete(scanner.nextInt());break;case "update":System.out.print("请输入更新员工的id:");id = scanner.nextInt();Emp byId = hashTab.findEmpById(id);if (byId == null) {System.out.println("没有此id的员工");} else {System.out.print("请输入更新后员工的姓名:");name = scanner.next();hashTab.change(new Emp(id, name));}break;case "exit":scanner.close();System.exit(0);default:System.out.println("输入的字符串不合法");break;}}}
}class HashTab {private EmpLinkedList[] empLinkedList;private int size;public HashTab(int size) {this.size = size;empLinkedList = new EmpLinkedList[size];}public void add(Emp emp) {int empNo = hashFun(emp.id);if (empLinkedList[empNo] == null) {empLinkedList[empNo] = new EmpLinkedList();}empLinkedList[empNo].add(emp);}public int hashFun(int id) {return id & (size - 1);}public void list() {for (int i = 0; i < size; i++) {if (empLinkedList[i] == null) {System.out.println("第 " + (i + 1) + " 条链表为空!");continue;}empLinkedList[i].list(i + 1);}}public Emp findEmpById(int id) {int hash = hashFun(id);if (empLinkedList[hash] == null) {return null;}return empLinkedList[hash].findById(id);}public void delete(int id) {int hash = hashFun(id);empLinkedList[hash].remove(id);}public void change(Emp emp) {int hash = hashFun(emp.id);empLinkedList[hash].update(emp);}
}class EmpLinkedList {//头指针,执行第一个Emp,因此这个链表的head是指向第一个Empprivate Emp head;public void update(Emp emp) {if (head == null) {System.out.println("此链表为空,不能修改");return;}Emp curEmp = head;while (true){if (curEmp.id == emp.id) {curEmp.name = emp.name;System.out.println("修改员工信息成功!");return;}curEmp = curEmp.next;if (curEmp == null) {System.out.println("此链表中没有此id的员工!");return;}}}public void add(Emp e) {if (head == null) {head = e;return;}Emp curEmp = head;while (curEmp.next != null) {curEmp = curEmp.next;}curEmp.next = e;}public void list(int i) {if (head == null) {System.out.println("第 " + i + " 条链表为空!");return;}System.out.print("第 " + i + " 条链表的信息为:");Emp curEmp = head;while (true) {System.out.print(curEmp + "\t=>\t");curEmp = curEmp.next;if (curEmp == null) {break;}}System.out.println();}public Emp findById(int id) {if (head == null) {return null;}Emp curEmp = head;while (true) {if (curEmp.id == id) {break;}curEmp = curEmp.next;if (curEmp == null) {break;}}return curEmp;}public void remove(int id) {if (head == null) {System.out.println("链表为空,无法删除!");return;}Emp curEmp = head;while (true) {if (head.id == id) {head = null;System.out.println("删除员工成功");return;}if (curEmp.next == null) {System.out.println("没有此员工,无法删除");return;}if (curEmp.next.id == id) {curEmp.next = curEmp.next.next;System.out.println("删除员工成功");return;}curEmp = curEmp.next;}}
}class Emp {public int id;public String name;public Emp next;public Emp(int id, String name) {this.id = id;this.name = name;}@Overridepublic String toString() {return "Emp{" +"id=" + id +", name='" + name + '\'' +'}';}
}

简单hashtab的实现相关推荐

  1. HashTab基于链表简单实现(java,不包含扩容)

    一.文件目录 二.代码 /*** 定义一个雇员*/ public class Emp {public int id;public String name;//默认为nullpublic Emp nex ...

  2. 【HashTab初学】哈希表

    看一个实际需求,有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的id时,要求查找到该员工的 所有信息. 要求: 不使用数据库,尽量节省内存,速度越 ...

  3. Java之HashTab基本用法

    表示一个雇员 public class Emp {public int id;public String name;public Emp next;//next 默认为 nullpublic Emp( ...

  4. 在docker上安装部署tomcat项目 超简单,拿来主义

    在docker中部署tomcat,非常简单,而且省去了手动安装jdk等步骤,只需要将war包复制在容器tomcat实例中的webapps下面即可.以下将详细讲解流程: 在windows中打好包以后用w ...

  5. Linux下tomcat的安装与卸载以及配置(超简单)

    无敌简单的几步 1.安装 //首先你需要下载好tomcat包 sudo tar -xvzf apache-tomcat-7.0.85.tar.gz(这里是包名) -C 你要放的位置 2.卸载 rm - ...

  6. Docker安装Apache与运行简单的web服务——httpd helloworld

    Docker运行简单的web服务--httpd helloworld目录[阅读时间:约5分钟] 一.Docker简介 二.Docker的安装与配置[CentOS环境] 三.Docker运行简单的web ...

  7. Docker的安装、镜像源更换与简单应用

    Docker的安装.镜像源更换与简单应用[阅读时间:约20分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 三.Docker的安装 四.Docker的简单应用 1. ...

  8. 基于Golang的简单web服务程序开发——CloudGo

    基于Golang的简单web服务程序开发--CloudGo[阅读时间:约10分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 (1)基本要求 (2)扩展要求 三.具体 ...

  9. 简单图文配置golang+vscode【win10/centos7+golang helloworld+解决install failed等情况】

    博客目录(阅读时间:10分钟) 一.win10 0.系统环境 1. win10配置golang环境 ①下载相关软件 ②创建gowork工作空间 ③配置环境变量(GOPATH+PATH) ④验证环境配置 ...

最新文章

  1. shiro处理ajax请求未登录,shiro处理ajax请求session失效跳转
  2. haproxy + keepalived “一键安装包” 配置与测试HOWTO
  3. leetcode算法题--在排序数组中查找数字 I
  4. Windows数据类型探幽——千回百转你是谁?(1)
  5. 安装Windows digits问题列表
  6. MVC5网站部署到IIS7
  7. selenium 定位方式5
  8. linux 内核高端内存意义,Linux内核高端内存管理
  9. 正则表达式入门教程-连载(2)-正则表达式引擎怎么工作的
  10. macOS下JetBrains配置修改错误导致无法启动解决方案
  11. Java 获取汉字拼音的方法
  12. 4.企业安全建设入门(基于开源软件打造企业网络安全) --- 威胁情报
  13. MapReduce在Map端的Combiner和在Reduce端的Partitioner
  14. roller java,月光软件站 - 编程文档 - Java - 修改ReadMorePlugin.java,使其支持中文标题(roller webblog)...
  15. 【tcp】网络连接中的长连接和短连接是什么意思?
  16. 用Canvas画一棵二叉树
  17. 北京大学灵异事件计算机房,北大 清华高校 发生的真实恐怖灵异事件
  18. 泰拉瑞亚服务器云存档文件夹,泰拉瑞亚服务器云存档文件
  19. RuoYi-Vue——关于登录后不同角色跳不同页面
  20. 动手写操作系统9----键盘鼠标中断实现

热门文章

  1. android换手机照片怎么转移,换手机了怎么把照片转移到新手机
  2. 零售商店订单数据分析
  3. Switch 无法 关联账号 this page cannot be displayed
  4. 产品经理 : 最全的运营数据指标解读
  5. Text to image论文精读GR-GAN:逐步细化文本到图像生成 GRADUAL REFINEMENT TEXT-TO-IMAGE GENERATION
  6. C语言内联汇编使用方法
  7. 基于单片机的信号发生器设计
  8. Redis(五)Redis内存维护方案(过期策略及淘汰策略)
  9. MCAL-GTM之时钟管理CMU
  10. 【正解】LaTex插入空白页