作者:斌哥第一次 | 来源:互联网 | 2023-09-25 13:53
packagehashtab;importjava.util.Scanner;publicclassHashTabDemo{publicstaticvoidmain(String[
![在这里插入图片描述](https://img.php1.cn/3cd4a/18ace/696/1d8e759bd3e6bbec.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNjA4NA==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/e62700fe09f8933e.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNjA4NA==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/8343fdbffb0056b5.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNjA4NA==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/443b30bb45e66690.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNjA4NA==,size_16,color_FFFFFF,t_70)
package hashtab;import java.util.Scanner;public class HashTabDemo {public static void main(String[] args) {HashTab hashTab = new HashTab(7);String key = "";Scanner sc = 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("exit:退出系统");key = sc.next();switch (key){case "add":System.out.println("输入id:");int id = sc.nextInt();System.out.println("输入名字:");String name = sc.next();Emp emp = new Emp(id,name);hashTab.add(emp);break;case "list":hashTab.list();break;case "find":System.out.println("请输入要查找的id:");id = sc.nextInt();hashTab.findEmpById(id);break;case "delete":System.out.println("请输入要删除的id:");id = sc.nextInt();hashTab.deleteEmpById(id);break;case "exit":sc.close();System.exit(0);default:break;}}}
}
class HashTab{private EmpLinkedList[] empLinkedListArray;private int size;public HashTab(int size) {this.size &#61; size;empLinkedListArray &#61; new EmpLinkedList[size];for (int i &#61; 0; i < size; i&#43;&#43;) {empLinkedListArray[i] &#61; new EmpLinkedList();}}public void add(Emp emp){int empLinkedListNO &#61; hashFun(emp.id);empLinkedListArray[empLinkedListNO].add(emp);}public void findEmpById(int id){int empLinkedListNO &#61; hashFun(id);Emp emp &#61; empLinkedListArray[empLinkedListNO].findEmpById(id);if (emp !&#61; null){System.out.printf("在第%d条链表中找到雇员 id&#61;%d,name&#61;%s",(empLinkedListNO&#43;1),emp.id,emp.name);}else {System.out.println("在哈希表中&#xff0c;没有找到该雇员~");}System.out.println();}public void deleteEmpById(int id){int empLinkedListNO &#61; hashFun(id);empLinkedListArray[empLinkedListNO].deleteEmpById(id);}public void list(){for (int i &#61; 0; i < size; i&#43;&#43;) {empLinkedListArray[i].list(i);}}public int hashFun(int id){return id % size;}
}
class Emp{public int id;public String name;public Emp next; public Emp(int id, String name) {this.id &#61; id;this.name &#61; name;}
}
class EmpLinkedList{private Emp head; public void add(Emp emp){if (head &#61;&#61; null){head &#61; emp;return;}Emp curEmp &#61; head;while(true){if (curEmp.next &#61;&#61; null){break;}curEmp &#61; curEmp.next;}curEmp.next &#61; emp;}
public void list(int no){if (head &#61;&#61; null){System.out.println("第"&#43; (no&#43;1) &#43;"条链表为空");return;}System.out.print("第"&#43; (no&#43;1) &#43;"的信息为&#xff1a;");Emp curEmp &#61; head;while (true){System.out.printf("&#61;> id&#61;%d name&#61;%s\t",curEmp.id,curEmp.name);if (curEmp.next &#61;&#61; null){break;}curEmp &#61; curEmp.next;}System.out.println();}public Emp findEmpById(int id){if (head &#61;&#61; null){System.out.println("链表为空");return null;}Emp curEmp &#61; head;while(true){if (curEmp.id &#61;&#61; id){break;}if (curEmp.next &#61;&#61; null){curEmp &#61; null;break;}curEmp &#61; curEmp.next;}return curEmp;}public void deleteEmpById(int id){if (head &#61;&#61; null){System.out.println("链表为空");return;}Emp curEmp &#61; head;while (true){if (curEmp.id &#61;&#61; id){head &#61; curEmp.next;return;}if (curEmp.next.id &#61;&#61; id){curEmp.next &#61; curEmp.next.next;break;}else {System.out.println("找不到需要删除雇员");}curEmp &#61; curEmp.next;}}
}