java实现哈希表
public class HashTabDemo {
public static void main(String []args){
HashTab hashTab = new HashTab(7); //实例化一个有7条链表的哈希表
String key = "";
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("add:添加雇员");
System.out.println("list:显示雇员");
System.out.println("exit:退出");
System.out.println("find: 查找");
System.out.println("delete: 删除");
key = scanner.next();
switch (key){
case "add":
System.out.println("请输入id:");
int id = scanner.nextInt();
System.out.println("请输入name:");
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.fidEmpById(id);
break;
case "delete":
System.out.println("输入要删除雇员的id:");
id = scanner.nextInt();
hashTab.deleteById(id);
default:
break;
}
}
}
}
//员工
class Emp{
public String name;
public int id;
public Emp next = null;
public Emp(int id,String name){
this.id = id;
this.name = name;
}
}
//创建哈希表管理多条链表
class HashTab{
private LinkList [] linkListArray;
int size;//表示有多少条链表
public HashTab(int size){ //初始化链表条数
this.size = size;
linkListArray = new LinkList[size];
for (int i = 0;i < size; i++){
linkListArray [i] = new LinkList();
}
}
//哈希表添加雇员
public void add(Emp emp){
int linkListNO = hashFun(emp.id); //根据员工的id判断添加到哪条链表
linkListArray[linkListNO].add(emp);
}
public int hashFun(int id){ //散列函数,
return id % size;
}
//遍历哈希表
public void list(){
for (int i = 0; i < size; i++){
linkListArray[i].list(i);
}
}
//输入id查找雇员
public void fidEmpById(int id){
int likListNO = hashFun(id);
Emp emp = linkListArray[likListNO].findEmpById(id);
if (emp ==null){
System.out.println("没找到");
}
else{
System.out.println("在第"+likListNO+"条链表找到员工"+"id:"+emp.id+" "+"name:"+emp.name);
}
}
//输入id删除雇员
public void deleteById(int id){
int linkListNO = hashFun(id);
linkListArray[linkListNO].deleteById(id);
}
}
//创建链表
class LinkList{
private Emp head = null;
// 添加雇员
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+"条链表为空");
return;
}
System.out.printf("第"+no+"条链表信息为:");
Emp curEmp = head;
while (true){
System.out.printf("->id:" + curEmp.id+" "+"name:" + curEmp.name+" ");
if (curEmp.next == null){ //遍历到最后节点退出循环
break;
}
curEmp = curEmp.next; //指针后移
}
System.out.println(); //输出第一条链表之后换行
}
//根据id查找雇员
//如果找到返回Emp,没找到返回空
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 deleteById(int id){
if (head == null){
System.out.println("链表为空");
return;
}
Emp curEmp = head;
while (true){
if (head.id == id){ //判断删除的是不是头节点
head = head.next;
System.out.println("删除成功");
break;
}else if (curEmp.next == null){ //下一个节点为空说明没有此id,下个节点不为空继续判断
System.out.println("要删除的雇员不存在");
break;
}else if (curEmp.next.id == id){
curEmp.next = curEmp.next.next; //删除指定id的节点
System.out.println("删除成功");
break;
}
curEmp = curEmp.next;
}
}
}