import java.util.Scanner; public class HashTabDemo { 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("exit:退出系统"); key = scanner.next(); switch (key) { case "add": System.out.println("输入id"); int id = scanner.nextInt(); System.out.println("输入name"); String name = scanner.next(); Emp emp = new Emp(id, name); hashTab.add(emp); break; case "list": hashTab.list(); break; case "find": System.out.println("请输入要查找的id"); int findId = scanner.nextInt(); hashTab.findById(findId); break; case "delete": System.out.println("请输入要删除的id"); findId = scanner.nextInt(); hashTab.deleteById(findId); break; case "exit": scanner.close(); System.exit(0); break; default: break; } } } } class HashTab { private int size; private EmpLinkedList[] empLinkedListArr; public HashTab(int size) { this.size = size; empLinkedListArr = new EmpLinkedList[size]; //分别初始化数组下标上的链表 for (int i = 0; i < empLinkedListArr.length; i++) { empLinkedListArr[i] = new EmpLinkedList(); } } public void deleteById(int id) { int no = hashFun(id); Emp emp = empLinkedListArr[no].findById(id); if (emp != null) { empLinkedListArr[no].deleteById(id); } else { System.out.println("无此id对应的雇员"); } } public void findById(int id) { int no = hashFun(id); Emp emp = empLinkedListArr[no].findById(id); if (emp != null) { System.out.printf("雇员信息name=%s", emp.getName()); System.out.println(); } else { System.out.println("在哈希表中没找到"); } } public void add(Emp emp) { int hashId = hashFun(emp.getId()); empLinkedListArr[hashId].add(emp); } public void list() { //这里不需要去判断了,直接放到empLinkedListArr中去判断即可 /* if (empLinkedListArr.length==0){ System.out.println("空的hash表"); return; }*/ for (int i = 0; i < empLinkedListArr.length; i++) { empLinkedListArr[i].list(); } } public int hashFun(int id) { return id % size; } } class EmpLinkedList { private Emp head;//初始化链表为空 public void deleteById(int id) { if (head == null) { System.out.println("链表为空"); return; } Emp temp = head; if (head.getId() == id) { head = head.next; return; } while (true) { if (temp.next == null) { break; } if (temp.next.getId() == id) { temp.next = temp.next.next; System.out.println("删除成功"); break; } temp = temp.next; } } public Emp findById(int id) { if (head == null) { System.out.println("链表为空"); return null; } Emp temp = head; while (true) { if (temp.getId() == id) { break; } if (temp.next == null) { return null; } temp = temp.next; } return temp; } public void add(Emp emp) { //如果是第一个 if (head == null) { head = emp; return; } //如果不是第一个雇员,那么创建一个辅助指针帮助我们遍历插入 Emp tempEmp = head; /* while (true){ if (tempEmp.next==null){ break; } tempEmp=tempEmp.next; }*/ while (true) { if (tempEmp.next == null) { break; } tempEmp = tempEmp.next; } tempEmp.next = emp; } public void list() { if (head == null) { System.out.println("当前链表为空"); return; } //当前链表信息为 Emp tempEmp = head; while (true) { System.out.printf("节点id=%d 名字name=%s", tempEmp.getId(), tempEmp.getName()); if (tempEmp.next == null) { break; } tempEmp = tempEmp.next; } System.out.println(); } } class Emp { private int id; private String name; public Emp getNext() { return next; } public void setNext(Emp next) { this.next = next; } Emp next;//默认为null public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Emp(int id, String name) { this.id = id; this.name = name; } }
手写哈希表
最新推荐文章于 2023-12-25 11:47:54 发布