雇员信息的增删改查
注意事项:给这个hash数组初始化的时候内外都要进行初始化
next和nextline的区别:next和nextline的注意事项
public HashTable(int size) {
empLinkedLists = new EmpLinkedList[size];
this.size = size;
for (int i = 0; i < empLinkedLists.length; i++) {
empLinkedLists[i] = new EmpLinkedList();
}
}
package com.hebut.liyiwei.hashTable;
import java.util.Scanner;
/**
* @className: HashTableDemo
* @author: Liyiwei
* @date: 2024/10/26 下午4:04
* @Version: 1.0
* @description: 哈希表
*/
public class HashTableDemo {
public static void main(String[] args) {
HashTable hashTable = new HashTable(7);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("add:增加雇员");
System.out.println("getnamebyid:查找雇员");
System.out.println("delnamebyid:删除雇员");
System.out.println("list:遍历雇员");
System.out.println("exit:退出程序");
String input = scanner.next();
switch (input) {
case "add":
System.out.println("请输入id");
int id = scanner.nextInt();
System.out.println("请输入名字");
String name = scanner.next();
hashTable.add(new Emp(id,name));
break;
case "getnamebyid":
System.out.println("请输入id");
int empid = scanner.nextInt();
String empName = hashTable.getEmpName(empid);
System.out.println(empName);
break;
case "delnamebyid":
System.out.println("请输入id");
int delid = scanner.nextInt();
hashTable.delEmpName(delid);
break;
case "list":
hashTable.display();
break;
case "exit":
scanner.close();
System.exit(0);
}
}
}
}
class HashTable {
private EmpLinkedList[] empLinkedLists;
public int size;
public HashTable(int size) {
empLinkedLists = new EmpLinkedList[size];
this.size = size;
for (int i = 0; i < empLinkedLists.length; i++) {
empLinkedLists[i] = new EmpLinkedList();
}
}
public void add(Emp emp) {
int index = getHash(emp);
empLinkedLists[index].addEmp(emp);
}
public void display() {
for (int i = 0; i < empLinkedLists.length; i++) {
if (empLinkedLists[i].getHead() == null) {
System.out.print("第"+(i+1)+"条链表为空");
}
empLinkedLists[i].display();
System.out.println();
}
}
public int getHash(Emp emp) {
int hash = emp.id % size;
return hash;
}
public int getHash(int id) {
int hash = id % size;
return hash;
}
public String getEmpName(int id) {
int index = getHash(id);
Emp emp = empLinkedLists[index].getHead();
if (emp == null) {
return null;
}
return empLinkedLists[index].getEmp(id).getName();
}
public void delEmpName(int id) {
int index = getHash(id);
Emp emp = empLinkedLists[index].getHead();
if (emp == null) {
System.out.println("该人员不存在");
}
empLinkedLists[index].delEmp(id);
}
}
class Emp{
public int id;
public String name;
public Emp next;
public Emp(int id, String name) {
super();
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
}
class EmpLinkedList{
private Emp head;
public void addEmp(Emp emp){
if(head == null){
head = emp;
}else {
Emp temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next = emp;
}
}
public Emp getEmp(int id){
Emp temp = head;
while(temp != null){
if(temp.id == id){
return temp;
}
}
return null;
}
public void delEmp(int id){
Emp temp = head;
if(temp.id == id){
head = head.next;
}
while(temp.next != null){
if(temp.next.id == id){
temp.next = temp.next.next;
}
}
}
public void display(){
Emp temp = head;
while(temp != null){
System.out.print("["+temp.id+","+temp.name+"] ");
temp = temp.next;
}
}
public Emp getHead() {
return head;
}
}