写在前面
哈希表
- 散列表,根据关键码值,进行数据访问,通过关键码值映射
散列函数
到表中一个位置来访问,存放数据的数组叫做散列表
散列函数
,就是决定数据放在哪条链表里的规则
- 哈希表,管理多条链表
class HashTable {
private EmployeeLinkedList[] employeeLinkedListArr;
private int size;
public HashTable(int size) {
employeeLinkedListArr = new EmployeeLinkedList[size];
this.size = size;
for (int i = 0; i < size; i++) {
employeeLinkedListArr[i] = new EmployeeLinkedList();
}
}
public void add(Employee employee) {
int employeeLinkedListNo = hashFunction(employee.id);
employeeLinkedListArr[employeeLinkedListNo].add(employee);
}
public void addByOrder(Employee employee) {
int employeeLinkedListNo = hashFunction(employee.id);
employeeLinkedListArr[employeeLinkedListNo].addByOrder(employee);
}
public void list() {
for (EmployeeLinkedList employeeLinkedList : employeeLinkedListArr) {
employeeLinkedList.list();
}
}
public void findEmployeeById(int id) {
int employeeLinkedListNo = hashFunction(id);
Employee employee = employeeLinkedListArr[employeeLinkedListNo].findEmployeeById(id);
if (employee != null) {
System.out.println(employee);
} else {
System.out.println("No find");
}
}
public void delEmployeeById(int id) {
int employeeLinkedListNo = hashFunction(id);
employeeLinkedListArr[employeeLinkedListNo].delEmployeeById(id);
}
private int hashFunction(int id) {
return id % size;
}
}
class Employee {
public int id;
public String name;
public Employee next;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public Employee() {
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + ''' +
'}';
}
}
class EmployeeLinkedList {
//头指针,指向第一个employee
private Employee head
//添加
public void add(Employee employee) {
//如果是第一个
if (head == null) {
head = employee
return
}
//定位到最后
Employee cur = head
while (cur.next != null) {
cur = cur.next
}
//加入
cur.next = employee
}
//顺序添加
public void addByOrder(Employee employee) {
//如果是第一个
if (head == null) {
head = employee
return
}
if (employee.id < head.id) {
employee.next = head
head = employee
return
} else if (employee.id == head.id) {
System.out.println("Exist")
return
}
//定位到最后
Employee cur = head
while (cur != null) {
if (cur.next == null) {
cur.next = employee
break
}
if (employee.id < cur.next.id) {
employee.next = cur.next
cur.next = employee
break
} else if (employee.id == cur.next.id) {
System.out.println("Exist")
break
}
cur = cur.next
}
}
//遍历
public void list() {
if (head == null) {
System.out.println("List is empty.")
return
}
Employee cur = head
while (cur != null) {
System.out.println(cur)
cur = cur.next
}
System.out.println("--------------")
}
public Employee findEmployeeById (int id) {
//判断链表是否为空
if (head == null) {
System.out.println("Empty")
}
Employee cur = head
while (true) {
if (cur.id == id) {
break
}
if (cur.next == null) {
cur = null
break
}
cur = cur.next
}
return cur
}
public void delEmployeeById (int id) {
if (head == null) {
System.out.println("Empty")
return
}
Employee cur = head
if (cur.id == id) {
head = head.next
return
}
while (true) {
if (cur.next == null) {
System.out.println("No find")
break
}
if (cur.next.id == id) {
cur.next = cur.next.next
break
}
cur = cur.next
}
}
}