hsp 哈希表 86-89
作者:
等一下
,
2022-02-04 18:08:58
,
所有人可见
,
阅读 180
hsp 哈希表 86-89
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main{
public static void main(String args[]) {
HashTab hashTab = new HashTab(7);
Scanner scanner = new Scanner(System.in);
String key = "";
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("输入名字");
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");
id = scanner.nextInt();
hashTab.findById(id);
break;
case "delete":
System.out.println("请输入要删除的id");
id = scanner.nextInt();
hashTab.delete(id);
break;
case "exit":
scanner.close();
System.exit(0);
}
}
}
}
class HashTab {
private LinkedList[] table;
private int size;
public HashTab(int size) {
this.size = size;
//注意!!这里只是初始化了数组,为数组分配了空间,数组的每个元素还是默认值
//如果数组的元素是引用类型,它的默认值就是null
//如果没有用for循环为数组的每个元素分配空间初始化,67行就会报空指针异常
table = new LinkedList[size];
//初始化数组的每个元素,为它们分配空间
for(int i = 0; i < size; i++) {
table[i] = new LinkedList();
}
}
public int hash(int id) {
return id % size;
}
public void add(Emp emp) {
int index = hash(emp.id);
table[index].add(emp);
}
//根据输入的id,查找雇员
public void findById(int id) {
int index = hash(id);
Emp emp = table[index].findById(id);
if(emp == null) {
System.out.println("在哈希表中,没有查找到该雇员");
} else {
System.out.printf("在[%d]链表中查找到该id=%d的雇员信息\n", index, id);
}
}
public void delete(int id) {
int index = hash(id);
boolean flag = table[index].delete(id);
if(flag) {
System.out.println("删除成功~");
} else {
System.out.println("删除失败");
}
}
public void list() {
for(int i = 0; i < size; i++) {
table[i].list(i);
}
}
}
class LinkedList {
private Emp head;
public void add(Emp emp) {
if(head == null) {
head = emp;
return;
}
Emp tmp = head;
while(true) {
if(tmp.next == null) {
break;
}
tmp = tmp.next;
}
tmp.next = emp;
}
//根据id查找雇员
//如果查找到,就返回那个对象,如果没找到,就返回null
public Emp findById(int id) {
if(head == null) {
System.out.print("链表为空,");
return null;
}
Emp tmp = head;
while(true) {
if(tmp.id == id) {
break;
}
if(tmp.next == null) {
tmp = null;
break;
}
tmp = tmp.next;
}
return tmp;
}
//根据id删除雇员
//如果找到,就返回true,如果没找到,就返回false
public boolean delete(int id) {
if(head == null) {
System.out.print("链表为空,");
return false;
}
Emp tmp = head;
boolean flag = false; //标志是否找到待删除雇员,初始默认没找到
while(true) {
if(tmp.id == id) {
flag = true;
break;
}
if(tmp.next == null) {
break;
}
tmp = tmp.next;
}
if(flag) {
tmp.id = tmp.next.id;
tmp.name = tmp.next.name;
tmp.next = tmp.next.next;
} else {
System.out.print("没有查找到该雇员,");
}
return flag;
}
public void list(int no) {
if(head == null) {
System.out.println("[" + no + "]链表为空");
return;
}
Emp tmp = head;
System.out.print("[" + no + "]链表:");
while(true) {
System.out.printf("-> id = %d, name = %s\t", tmp.id, tmp.name);
if(tmp.next == null) {
break;
}
tmp = tmp.next;
}
System.out.println();
}
}
class Emp {
public int id;
public String name;
public Emp next;
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
}