哈 希 表

散列表Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列

 

package com.wang;

import java.util.Scanner;

public class HashTable {

	public static void main(String[] args) {
		
		Hash hashT = new Hash(7);
		//菜单
		String key="";
		Scanner sc=new Scanner(System.in);
		while(true) {
			System.out.println("a:添加员工");
			System.out.println("l:显示员工");
			System.out.println("f:查找员工");
			System.out.println("d:删除员工");
			System.out.println("e:退出系统");
			
			key = sc.next();
			switch (key) {
			case "a":
				System.out.print("id:");
				int id = sc.nextInt();
				System.out.print("名字:");
				String name = sc.next();
				Employee employee = new Employee(id, name);
				hashT.add(employee);
				break;
				
			case "l":
				hashT.list();
				break;
				
			case "f":
				System.out.print("id:");
				int id1 = sc.nextInt();
				hashT.find(id1);
				break;
				
			case "d":
				System.out.print("id:");
				int id2 = sc.nextInt();
				hashT.del(id2);
				break;
				
			case "e":
				sc.close();
				System.exit(0);
			
			}
		}
		
	}

}


//创建Hash表,管理多条链表
class Hash{
	private EmployeeList[] emplist;
	//链表的数量
	private int size;
	
	
	//构造器
	public Hash(int size) {
		this.size = size;
		emplist=new EmployeeList[size];
		//初始化每一条链表!!!
		for (int i = 0; i < size; i++) {
			emplist[i] = new EmployeeList();
		}
	}
	
	
	//添加
	public void add(Employee e) {
		//根据员工id添加到对应的链表中
		int num=Hashfun(e.id);
		emplist[num].add(e);
	}
	
	
	//编写散列函数,使用取模法
	public int Hashfun(int id) {
		return id%size;
	}
	
	
	//遍历所有的链表
	public void list() {
		for (int i = 0; i < size; i++) {
			emplist[i].list(i);
		}
	}
	
	
	//根据输入的ID查找雇员
	public void find(int id) {
		int num=Hashfun(id);
		Employee emp=emplist[num].find(id);
		if (emp != null) {
			System.out.println("id:"+emp.id+"name:"+emp.name);
		}else {
			System.out.println("无");
		}
	}
	
	
	//根据输入的ID删除雇员
		public void del(int id) {
			int num=Hashfun(id);
			emplist[num].del(id);
		}
}


//员工类
class Employee{
	public int id;
	public String name;
	public Employee next;
	public Employee(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
}



//链表
class EmployeeList{
	private Employee head;
	
	//添加
	public void add(Employee e) {
		//添加第一个员工
		if (head == null) {
			head = e;
			return;
		}
		//不是第一个员工,则定位到最后
		Employee temp=head;
		while (true) {
			if (temp.next == null) {
				break;
			}
			temp = temp.next;
		}
		temp.next = e;
	}
	
	//遍历
	public void list(int no) {
		if (head == null) {
			System.out.println("第"+(no+1)+"条链表为空");
			return;
		}
		Employee temp = head;
		while(true) {
			System.out.print("->id:"+temp.id+"  name:"+temp.name);
			if (temp.next == null) {
				break;
			}
			temp = temp.next;
		}
		System.out.println();
	}
	
	
	//根据ID查找员工,找不到返回null
		public Employee find(int id) {
			if (head==null) {
				System.out.println("链表为空");
				return null;
			}
			Employee temp=head;
			while (true) {
				if (temp.id == id) {
					break;
				}
				if (temp.next == null) {
					temp=null;
					break;
				}
				temp = temp.next;
			}
			return temp;
		}
		
		//根据ID删除员工,找不到返回null
				public void del(int id) {
					if (head==null) {
						System.out.println("链表为空");
						return;
					}
					Employee temp=head;
					while (true) {
						//如果要删除的是头节点
						if (head.id == id) {
							head = head.next;
							return;
						}
						//如果要删除的是子节点
						if (temp.next.id == id) {
							temp.next=temp.next.next;
						}
						if (temp.next == null) {
							break;
						}
						temp = temp.next;
					}
				}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值