视频来自B站尚硅谷韩顺平的Java数据结构与java算法教程的第88和第89p内容
地址:https://www.bilibili.com/video/BV1E4411H73v?p=89
在此基础上完成了课后习题:增加顺序添加员工及按Id删除员工的功能(没有涉及修改),相当于再复习一遍链表,完整代码如下,已本地运行:
1 package hashmap; 2 3 import java.util.Scanner; 4 5 public class HashTab { 6 public static void main(String[] args){ 7 Hash hashTab = new Hash(7); 8 String key = ""; 9 Scanner scanner = new Scanner(System.in); 10 while (true){ 11 System.out.println("add: 添加雇员"); 12 System.out.println("list: 显示雇员"); 13 System.out.println("find: 查找雇员"); 14 System.out.println("del: 删除雇员"); 15 System.out.println("exit: 退出系统"); 16 key = scanner.next(); 17 switch (key){ 18 case "add": 19 System.out.println("输入id"); 20 int id = scanner.nextInt(); 21 System.out.println("输入名字"); 22 String name = scanner.next(); 23 Emp emp = new Emp(id,name); 24 hashTab.add(emp); 25 break; 26 case "list": 27 hashTab.list(); 28 break; 29 case "find": 30 System.out.println("请输入要查找的id"); 31 id = scanner.nextInt(); 32 hashTab.findEmpById(id); 33 break; 34 case "del": 35 System.out.println("请输入要删除的id"); 36 id = scanner.nextInt(); 37 hashTab.delEmpById(id); 38 break; 39 case "exit": 40 scanner.close(); 41 System.exit(0); 42 default: 43 break; 44 } 45 } 46 47 } 48 } 49 50 class Hash{ 51 private EmpLinkedList[] empLinkedListArray; 52 private int size; 53 54 public Hash(int size){ 55 this.size = size; 56 empLinkedListArray = new EmpLinkedList[size]; 57 for (int i = 0; i < size; i++){ 58 empLinkedListArray[i] = new EmpLinkedList(); 59 } 60 } 61 62 public void add(Emp emp){ 63 int empLinkedListNo = hashFun(emp.id); 64 empLinkedListArray[empLinkedListNo].orderAdd(emp); 65 } 66 public void list(){ 67 for (int i = 0; i < size; i++){ 68 empLinkedListArray[i].list(i); 69 } 70 } 71 72 public void findEmpById(int id){ 73 int empLinkedListNO = hashFun(id); 74 Emp emp = empLinkedListArray[empLinkedListNO].findEmpById(id); 75 if (emp != null){ 76 System.out.printf("在第%d条链表中找到该雇员,id=%d ",(empLinkedListNO+1),id); 77 System.out.println("该雇员的信息为:id= "+emp.id+" name= "+emp.name); 78 }else { 79 System.out.println("在哈希表中没有找到该雇员"); 80 } 81 } 82 83 public void delEmpById(int id){ 84 int empLinkedListNO = hashFun(id); 85 empLinkedListArray[empLinkedListNO].delEmpById(id); 86 } 87 88 public int hashFun(int id){ 89 //散列函数 90 return id % size; 91 } 92 } 93 94 class Emp{ 95 public int id; 96 public String name; 97 public Emp next; 98 public Emp(int id, String name){ 99 super(); 100 this.id = id; 101 this.name = name; 102 } 103 } 104 105 106 class EmpLinkedList{ 107 private Emp head; 108 public void add(Emp emp){ 109 //按顺序添加 110 if(head == null){ 111 head = emp; 112 return; 113 } 114 Emp curEmp = head; 115 while (true){ 116 if(curEmp.next == null){ 117 break; 118 } 119 curEmp = curEmp.next; 120 } 121 curEmp.next = emp; 122 } 123 124 public void orderAdd(Emp emp){ 125 //按顺序添加员工信息 126 if (head == null){ 127 //空链表 128 head = emp; 129 return; 130 } 131 if (head.id > emp.id){ 132 //如果头结点的id大于要插入的id,则将emp指向当前头结点,然后再把头结点置为emp 133 emp.next = head; 134 head = emp; 135 return; 136 } 137 if (head.id == emp.id){ 138 //如果头结点的id等于要加入的id,则提示不能加入 139 System.out.printf("第%d号员工已经存在,不能加人 ", emp.id); 140 return; 141 } 142 Emp curEmp = head; 143 boolean flag = false; 144 while (true){ 145 if(curEmp.next == null){ 146 break; 147 } 148 if(curEmp.next.id > emp.id){ 149 break; 150 }else if (curEmp.next.id == emp.id){ 151 flag = true; 152 break; 153 } 154 curEmp = curEmp.next;//后移,遍历 155 } 156 if(flag){ 157 System.out.printf("第%d号员工已经存在,不能加人 ", emp.id); 158 }else{ 159 if (curEmp.next == null){ 160 //如果当前链表只有一个节点,则直接添加到最后 161 //head = emp; 162 curEmp.next = emp; 163 }else { 164 //如果要插入的位置在节点中间 165 emp.next = curEmp.next; 166 curEmp.next = emp; 167 } 168 169 } 170 } 171 172 public void list(int no){ 173 if(head == null){ 174 System.out.println("第"+(no + 1)+"条链表为空"); 175 return; 176 } 177 System.out.println("第"+(no+1)+"条链表的信息为:"); 178 Emp curEmp = head; 179 while (true){ 180 System.out.printf(" => id=%d name=%s ",curEmp.id,curEmp.name); 181 if(curEmp.next == null){ 182 break; 183 } 184 curEmp = curEmp.next; 185 } 186 System.out.println(); 187 } 188 189 public Emp findEmpById(int id){ 190 if (head == null){ 191 System.out.println("链表为空"); 192 return null; 193 } 194 Emp curEmp = head; 195 while (true){ 196 if (curEmp.id == id){ 197 break; 198 } 199 if(curEmp.next == null){ 200 curEmp = null; 201 break; 202 } 203 curEmp = curEmp.next; 204 } 205 return curEmp; 206 } 207 208 public void delEmpById(int id){ 209 //按照id删除员工信息 210 if (head == null){ 211 System.out.println("链表为空"); 212 return; 213 } 214 if (head.id == id) { 215 // 如果头结点的id等于要删除的id,则将头结点指向下一个 216 head = head.next; 217 //head = null; 218 return; 219 } 220 221 Emp curEmp = head; 222 boolean flag = false; 223 while (true) { 224 if (curEmp.next == null) { 225 break; 226 } 227 if (curEmp.next.id == id) { 228 flag = true; 229 break; 230 } 231 curEmp = curEmp.next;//后移,遍历 232 } 233 if (flag) { 234 curEmp.next = curEmp.next.next; 235 }else { 236 System.out.println("没有找到第" + id + "号员工"); 237 } 238 } 239 }