笨办法学数据结构 哈希表的介绍与实现

l 希表的基本介绍

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

15   111 % 15

代码实现:

package com.liu.hashtab;

import java.util.Scanner;

public class hashtabDemo {
	public static void main(String[] args) {
		Hashtabe hashTab = new Hashtabe(7);

		// 写一个简单的菜单
		String key = "";
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("add:  添加雇员");
			System.out.println("list: 显示雇员");
			System.out.println("find: 查找雇员");
			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 "exit":
				scanner.close();
				System.exit(0);
			default:
				break;
			}
		}

	}
}

class Hashtabe {
	private EmpLinkedList[] empLinkedListArray;
	private int size;

	public Hashtabe(int size) {
		this.size = size;
		empLinkedListArray = new EmpLinkedList[size];
		for (int i = 0; i < size; i++) {
			empLinkedListArray[i] = new EmpLinkedList();
		}
	}

	public void add(Emp emp) {
		int arrayno = haveFun(emp.id);
		empLinkedListArray[arrayno].add(emp);
	}

	public void list() {
		for (int i = 0; i < empLinkedListArray.length; i++) {
			empLinkedListArray[i].list(i);
		}
	}

	public void findbyid(int id) {
		int empLinkedListNO = haveFun(id);
		Emp emp = empLinkedListArray[empLinkedListNO].findbyid(id);
		if (emp != null) {
			System.out.printf("在第%d条链表中找到 雇员 id = %d\n", (empLinkedListNO + 1), id);
		} else {
			System.out.println("在哈希表中,没有找到该雇员~");
		}
	}

	public int haveFun(int id) {
		return id % size;
	}

}

class EmpLinkedList {
	private Emp head;

	public void add(Emp emp) {
		if (head == null) {
			head = emp;
			return;
		}
		Emp cur = head;
		while (cur.next != null) {
			cur = cur.next;
		}
		cur.next = emp;
	}

	public void list(int no) {
		if (head == null) {
			System.out.println("第" + (no + 1) + "条链表为空");
			return;
		}
		Emp cur = head;
		System.out.print("第 " + (no + 1) + " 链表的信息为");
		while(true) {
			System.out.printf(" => id=%d name=%s\t", cur.id, cur.name);
			if(cur.next == null) {//说明curEmp已经是最后结点
				break;
			}
			cur = cur.next; //后移,遍历
		}
		System.out.println();

	}

	public Emp findbyid(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;
	}
}

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;
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一只猪的思考

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

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

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

打赏作者

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

抵扣说明:

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

余额充值