生活随笔
收集整理的這篇文章主要介紹了
数据结构 - 哈希表(用数组+链表实现存储员工信息,添加增删查功能)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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:退出系統(tǒng)");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("請(qǐng)輸入要查找的id:");id
= sc
.nextInt();hashTab
.findEmpById(id
);break;case "delete":System
.out
.println("請(qǐng)輸入要?jiǎng)h除的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
= 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 findEmpById(int id
){int empLinkedListNO
= hashFun(id
);Emp emp
= empLinkedListArray
[empLinkedListNO
].findEmpById(id
);if (emp
!= null
){System
.out
.printf("在第%d條鏈表中找到雇員 id=%d,name=%s",(empLinkedListNO
+1),emp
.id
,emp
.name
);}else {System
.out
.println("在哈希表中,沒有找到該雇員~");}System
.out
.println();}public void deleteEmpById(int id
){int empLinkedListNO
= hashFun(id
);empLinkedListArray
[empLinkedListNO
].deleteEmpById(id
);}public void list(){for (int i
= 0; i
< size
; i
++) {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
= id
;this.name
= name
;}
}
class EmpLinkedList{private Emp head
; public void add(Emp emp
){if (head
== null
){head
= emp
;return;}Emp curEmp
= head
;while(true){if (curEmp
.next
== null
){break;}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\t",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 void deleteEmpById(int id
){if (head
== null
){System
.out
.println("鏈表為空");return;}Emp curEmp
= head
;while (true){if (curEmp
.id
== id
){head
= curEmp
.next
;return;}if (curEmp
.next
.id
== id
){curEmp
.next
= curEmp
.next
.next
;break;}else {System
.out
.println("找不到需要?jiǎng)h除雇員");}curEmp
= curEmp
.next
;}}
}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的数据结构 - 哈希表(用数组+链表实现存储员工信息,添加增删查功能)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。