尚硅谷数据结构与算法(Java)--20--哈希表

:哈希表


package com.atguigu.hashtable;

import java.util.Scanner;


//哈希表
public class HashTabDemo {

	public static void main(String[] args) {
		//创建哈希表
		HashTap hashTap = new HashTap(7);
		//写一个菜单
		String key = "";
		Scanner sc = new Scanner(System.in);
		while(true) {
			System.out.println("add:添加雇员");
			System.out.println("list:显示雇员");
			System.out.println("find:查找雇员");
			System.out.println("del:删除雇员");
			System.out.println("exit:退出系统");
			key = sc.next();
			switch(key) {
			case "add":
				System.out.println("输入id");
				int id = sc.nextInt();
				System.out.println("输入名字");
				String name = sc.next();
				//创建雇员
				Emp emp = new Emp(id,name);
				hashTap.add(emp);
				break;
			case "list":
				hashTap.list();
				break;
			case "find":
				System.out.println("请输入要查找的id");
				id = sc.nextInt();
				hashTap.findEmpById(id);
				break;
			case "del":
				System.out.println("请输入要删除的id");
				id = sc.nextInt();
				hashTap.delById(id);
				break;
			case "exit":
				sc.close();
				System.exit(0);
			default:
				System.out.println("输入有误");
				break;
			}
		}
		
	}

}

//创建HashTab 管理多条链表
class HashTap {
	private EmpLinkedList[] empLinkedListArray;
	private int size;
	// 构造器
	public HashTap(int size) {
		this.size = size;
		// 初始化empLinkedListArray
		empLinkedListArray = new EmpLinkedList[size];
		//  ??一个坑 ,  不要忘了分别初始化每个链表
		for(int i=0;i<size;i++) {
			empLinkedListArray[i] = new EmpLinkedList();
		}
	}

	// 添加雇员
	public void add(Emp emp) {
		// 根据员工id,得到该员工莹添加到哪条链表
		int empLinkedListNO = hashFun(emp.id);
		// 将emp添加到对应的链表
		empLinkedListArray[empLinkedListNO].add(emp);
	}
	

	
	//遍历所有链表,遍历hashtab
	public void list() {
		for(int i=0;i<size;i++) {
			empLinkedListArray[i].list(i);
		}
	}

	//根据输入的id,查找雇员
	public void findEmpById(int id) {
		//使用散列表函数确定哪条链表创查找
		int empLinkedListNO = hashFun(id);
		Emp emp = empLinkedListArray[empLinkedListNO].finEmpById(id);
		if(emp != null) {
			System.out.printf("在第"+(empLinkedListNO+1)+"条链表中找到雇员id = %d\n",id);
		}else {
			System.out.println("在哈希表中没有找到该雇员。");
		}
	}
	
	//删除雇员
	public void delById(int id) {
		//使用散列表函数确定哪条链表查找
		int empLinkedListNO = hashFun(id);
		//操作
		empLinkedListArray[empLinkedListNO].delEmp(id);
	}
	
	// 编写散列函数,使用一个简单取模法
	public int hashFun(int id) {
		return id % size;
	}

}

//表示一个雇员
class Emp {
	public int id;
	public String name;
	public Emp next;// next默认null

	public Emp(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
}

//创建EmpLinkedList,表示链表
class EmpLinkedList {
	// 头指针,执行第一个Emp,因此该链表的head 是直接指向第一个Emp
	private Emp head;// 默认null
	/*
	 * 添加 Add 1.假定,当添加雇员时,id是 自增长,即id的分配总是从小到大 因此我们将该雇员直接加入到本链表的最后即可
	 */

	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;// 退出时将emp 加入链表
	}

	// 遍历链表的雇员信息
	public void list(int no) {
		if (head == null) {// 说明链表为空
			System.out.println("第"+(no+1)+"链表为空");
			return;
		}
		System.out.printf("第"+(no+1)+"链表的信息为:");
		Emp curEmp = head;
		while (true) {
			System.out.printf(">>> id=%d  name=%s\t", curEmp.id, curEmp.name);
			if (curEmp.next == null) {
				break;
			}
			curEmp = curEmp.next;
		}
		System.out.println();

	}
	//根据id查找雇员
	//找到就返回emp,没找到返回null
	public Emp finEmpById(int id) {
		//判断链表是否空
		if(head == null) {
			System.out.println("链表空");
			return null;
		}
		//辅助指针
		Emp curEmp = head;
		while(true) {
			if(curEmp.id == id) {//找到
				break;//此时curEmp指向要查找的雇员
			}
			//退出条件
			if(curEmp.next == null) {//说明遍历完没有找到
				curEmp = null;
				break;
			}
			curEmp = curEmp.next;//后移
		}
		return curEmp;
	}

    public void delEmp(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        Emp temp=head;
        while (true){
            if (head.id==id){
                head=head.next;
                return;
            }
            if (temp.next==null){//链表尾
                break;
            }
            if (temp.next.id==id){
                temp.next=temp.next.next;
                break;
            }else {
                System.out.println("没找到要删除的员工");
            }
            temp=temp.next;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值