哈希表实现——链表形式——人员管理系统
哈希表实现——链表形式——人员管理系统
一.简介
Java写的,一边学一边敲,比较简陋,当做个纪念。
二.代码
package com.appletree24.hashtable; import javax.swing.plaf.synth.SynthOptionPaneUI; import java.util.Scanner; public class HashTableDemo { public static void main(String[] args) { HashTable hashTable=new HashTable(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("del:删除雇员"); System.out.println("exit:退出系统"); key=scanner.next(); switch (key){ case "add": System.out.println("输入id"); int id=scanner.nextInt(); System.out.println("输入名字"); String name=scanner.next(); Emp emp=new Emp(id,name); hashTable.add(emp); break; case "del": System.out.println("输入id"); id=scanner.nextInt(); hashTable.deleteEmp(id); break; case "list": hashTable.list(); break; case "exit": scanner.close(); break; case "find": System.out.println("请输入要查找的雇员ID"); id=scanner.nextInt(); hashTable.findEmpById(id); break; default: break; } } } } class HashTable{ private EmpLinkedList[] empLinkedListArray; private int size; public HashTable(int size){ this.size=size; empLinkedListArray=new EmpLinkedList[size]; for (int i=0;i<size;i++){ empLinkedListArray[i]=new EmpLinkedList(); } } public void add(Emp emp){ int empLinkedListNO=hashFun(emp.id); empLinkedListArray[empLinkedListNO].add(emp); } public void list(){ for(int i=0;i<size;i++){ empLinkedListArray[i].list(i); } } public int hashFun(int id){ return id%size; } public boolean findEmpById(int id){ int empLinkedListNO=hashFun(id); Emp emp=empLinkedListArray[empLinkedListNO].findEmpById(id); if(emp!=null){ System.out.println("找到在"+(empLinkedListNO+1)+"链表中"+","+"雇员ID为:"+id); return true; }else{ System.out.println("哈希表中不存在此雇员"); return false; } } public void deleteEmp(int id){ int empLinkedListNO = hashFun(id); Emp emp = empLinkedListArray[empLinkedListNO].findEmpById_DeVer(id); if (emp.next == null) { empLinkedListArray[empLinkedListNO].head=null; } else { if (emp == null) { System.out.println("查无此人"); } else { if (emp.next.next != null) { System.out.println("删除成功"); emp.next = emp.next.next; } else { System.out.println("删除成功"); emp.next = null; } } } } } class Emp{ public int id; public String name; public Emp next; public Emp(int id, String name) { super(); this.id = id; this.name = name; } } class EmpLinkedList{ public Emp head; //添加第一个成员 public void add(Emp emp){ if(head==null){ head=emp; return; } //如果不是第一个雇员,使用辅助指针,定位到最后 Emp curEmp=head; while (true){ if(curEmp.next==null){ break; }else{ curEmp=curEmp.next; } } curEmp.next=emp; } public void list(int no){ if(head==null){ System.out.println("链表"+(no+1)+"为空"); return; } System.out.print((no+1)+"链表的信息为:"); Emp curEmp=head; while (true){ System.out.printf("=>id=%d name=%s",curEmp.id,curEmp.name); if(curEmp.next==null){ //最后一个节点 break; } curEmp=curEmp.next; } System.out.println(); } public Emp findEmpById(int id){ if(head==null){ System.out.println("链表为空"); return null; } Emp curEmp=head; while (true){ if(curEmp.id==id){ break; } if(curEmp.next==null){ curEmp=null; break; } curEmp=curEmp.next; } return curEmp; } //传入要删除结点的上一个节点 public boolean deleteEmp(Emp emp){ if(head==null){ System.out.println("链表为空,无需删除"); return false; }else if(emp==null){ System.out.println("参数错误"); return false; } emp.next=emp.next.next; return true; } public Emp findEmpById_DeVer(int id){ if(head==null){ System.out.println("链表为空"); return null; } if(head.id==id){ return head; } Emp curEmp=head; while(curEmp.next!=null){ if(curEmp.next.id==id){ return curEmp; } curEmp=curEmp.next; } return null; } }
本文作者:Appletree24
本文链接:https://www.cnblogs.com/appletree24/p/16484123.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步