/**
* @author chenyi
* @Description 自定義hashtable保存員工信息
* @date 2022/2/17 11:12
*/
public class MyHashtable {
int capacity = 11;
private Node[] data;
public MyHashtable() {
data = new Node[11];
}
private int hash(int id) {
return id % capacity;
}
public void add(Employee employee) {
int hashCode = hash(employee.getId());
Node newNode = new Node(employee);
Node head = data[hashCode];
if (head == null) {
data[hashCode] = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public Employee find(int id) {
int hashCode = hash(id);
Node curr = data[hashCode];
while (curr != null) {
if (curr.getEmployee()
.getId() == id) {
return curr.getEmployee();
}
curr = curr.next;
}
return null;
}
public void list() {
for (int i = 0; i < data.length; i++) {
Node curr = data[i];
System.out.print("第" + (i + 1) + "條鏈表:");
while (curr != null) {
System.out.print(curr.getEmployee()
.toString());
curr = curr.next;
}
System.out.println();
}
}
private Employee remove(int id) {
int hashCode = hash(id);
Node curr = data[hashCode];
Node prev = null;
while (curr != null) {
if (curr.getEmployee()
.getId() == id) {
if(prev==null) {
data[hashCode] = curr.next;
}else{
prev.next = curr.next;
}
curr.next = null;
return curr.employee;
}
prev = curr;
curr = curr.next;
}
return null;
}
class Node {
private Employee employee;
private Node next;
public Node(Employee employee) {
this.employee = employee;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Employee getEmployee() {
return employee;
}
}
public static void main(String[] args) {
MyHashtable myHashtable = new MyHashtable();
// 添加員工
System.out.println("添加測試");
myHashtable.add(new Employee(0,"jet"));
myHashtable.add(new Employee(1,"jack"));
myHashtable.add(new Employee(2,"smith"));
myHashtable.add(new Employee(3,"jane"));
myHashtable.add(new Employee(4,"jerry"));
myHashtable.add(new Employee(11,"hover"));
myHashtable.list();
System.out.println("刪除測試");
myHashtable.remove(0);
myHashtable.list();
myHashtable.remove(0);
myHashtable.list();
myHashtable.remove(11);
myHashtable.list();
System.out.println("查詢測試");
Employee employee = myHashtable.find(4);
System.out.println(employee);
}
}
class Employee {
private int id;
private String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return "{" + "id=" + id + ", name='" + name + '\'' + '}';
}
}
自定義hash表實(shí)現(xiàn)員工保存
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 適合場景 在使用Spring Data JPA的時(shí)候,在使用UUID作為主鍵的時(shí)候,通常都會使用system-uu...
- 在開發(fā)中,會經(jīng)常遇到拷貝數(shù)據(jù)對象的情況,也會遇到保存數(shù)據(jù)到本地的情況,如果是用OC的話,無論是copyWithZo...
- python 日志模塊 logging FileHandler: 以“a”(追加)的方式將日志輸出到文件,如果文件...
- 在上篇中,我與大家分享了關(guān)于如何進(jìn)行*.lrc歌詞文件的解析,以及將解析完成后的歌詞展示在鑲嵌在ScrollVie...