1.哈希表(散列)-Google 上机题
- 看一个实际需求,google 公司的一个上机题:
- 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的id时,要求查找到该员工的 所有信息.
- 要求: 不使用数据库,尽量节省内存,速度越快越好=>哈希表(散列)
2.哈希表的基本介绍
散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
3.google 公司的一个上机题
有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址..),当输入该员工的 id 时,要求查找到该员工的 所有信息
要求:
-
不使用数据库,,速度越快越好=>哈希表(散列)
-
添加时,保证按照 id 从低到高插入 [课后思考: 如果 id 不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?]
-
使用链表来实现哈希表, 该链表不带表头[即: 链表的第一个结点就存放雇员信息]
-
思路分析并画出示意图
-
代码实现(韩老师)
import java.util.Scanner;
public class HashTabDemo {
public static void main(String[] args) {
//创建哈希表
HashTab hashTab = new HashTab(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.findEmpById(id);
break;
case "exit":
scanner.close();
System.exit(0);
default:
break;
}
}
}
}
//创建 HashTab 管理多条链表
class HashTab {
private EmpLinkedList[] empLinkedListArray;
private int size; //表示有多少条链表
//构造器
public HashTab(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].findEmpById(id);
if (emp != null) {//找到
System.out.printf("在第%d 条链表中找到 雇员 id = %d\n", (empLinkedListNO + 1), id);
} else {
System.out.println("在哈希表中,没有找到该雇员~");
}
}
//编写散列函数, 使用一个简单取模法
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
//添加雇员到链表
//说明
//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; //后移
}
//退出时直接将 emp 加入链表
curEmp.next = emp;
}
//遍历链表的雇员信息
public void list(int no) {
if (head == null) { //说明链表为空
System.out.println("第 " + (no + 1) + " 链表为空");
return;
}
System.out.print("第 " + (no + 1) + " 链表的信息为");
Emp curEmp = head; //辅助指针
while (true) {
System.out.printf(" => id=%d name=%s\t", curEmp.id, curEmp.name);
if (curEmp.next == null) {//说明 curEmp 已经是最后结点
break;
}
curEmp = curEmp.next; //后移,遍历
}
System.out.println();
}
//根据 id 查找雇员
//如果查找到,就返回 Emp, 如果没有找到,就返回 null
public Emp findEmpById(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;
}
}
- 代码实现(自己)
import java.util.Scanner;
public class MyHashTabDemo {
public static void main(String[] args) {
EmpHashTab hashTab = new EmpHashTab(7);
while (true) {
System.out.println("list:展示");
System.out.println("add:添加");
System.out.println("del:删除");
System.out.println("find:查找");
System.out.println("exit:退出");
Scanner scanner = new Scanner(System.in);
String oper = scanner.next();
int id;
switch (oper) {
case "list":
hashTab.list();
break;
case "add":
System.out.println("输入id");
id = scanner.nextInt();
System.out.println("输入姓名");
String name = scanner.next();
hashTab.add(new Emp(id, name));
break;
case "del":
System.out.println("输入id");
id = scanner.nextInt();
hashTab.delete(id);
break;
case "find":
System.out.println("输入id");
id = scanner.nextInt();
hashTab.find(id);
break;
case "exit":
scanner.close();
System.exit(0);
default:
break;
}
}
}
/**
* 哈希表
*/
static class EmpHashTab {
private EmpLinkedList[] empLinkedListArray;
private int size;
public EmpHashTab(int size) {
this.size = size;
empLinkedListArray = new EmpLinkedList[size];
for (int i = 0; i < size; i++) {
empLinkedListArray[i] = new EmpLinkedList();
}
}
/**
* 添加
*
* @param emp
*/
public void add(Emp emp) {
int index = hashIndex(emp.id);
empLinkedListArray[index].add(emp);
}
/**
* 遍历
*/
public void list() {
for (int i = 0; i < size; i++) {
empLinkedListArray[i].list(i + 1);
}
}
/**
* 查找
*
* @param id
*/
public void find(int id) {
int index = hashIndex(id);
Emp emp = empLinkedListArray[index].find(id);
if (emp == null) {
System.out.println("没有找到id=" + id + "的员工");
} else {
System.out.println(emp);
}
}
/**
* 删除
*
* @param id
*/
public void delete(int id) {
int index = hashIndex(id);
empLinkedListArray[index].delete(id);
}
/**
* 哈希函数
*
* @param id
* @return
*/
public int hashIndex(int id) {
return id % size;
}
}
/**
* 链表
*/
static class EmpLinkedList {
private Emp head;
/**
* 添加
*/
public void add(Emp emp) {
if (head == null) {
head = emp;
return;
} else {
Emp cur = head;
while (cur.next != null) {
if (cur.id == emp.id) {
System.out.println("id=" + emp.id + "已存在不可以重复插入");
return;
}
cur = cur.next;
}
if (cur.id == emp.id) {
System.out.println("id=" + emp.id + "已存在不可以重复插入");
return;
}
cur.next = emp;
}
}
/**
* 遍历
*/
public void list(int no) {
if (head == null) {
System.out.println("第" + no + "队列为空");
return;
}
Emp cur = head;
System.out.print("第" + no + "队列");
while (cur != null) {
System.out.print("==>" + cur);
cur = cur.next;
}
System.out.println();
}
/**
* 查找
*
* @param id
* @return
*/
public Emp find(int id) {
if (head == null) {
System.out.println("链表为空");
return null;
}
Emp cur = head;
while (cur != null) {
if (cur.id == id) {
return cur;
}
cur = cur.next;
}
return null;
}
/**
* 删除
*
* @param id
*/
public void delete(int id) {
if (head == null) {
System.out.println("链表为空");
return;
}
if (head.id == id) {
head = head.next;
return;
}
Emp cur = head;
while (cur.next != null) {
if (cur.next.id == id) {
cur.next = cur.next.next;
return;
}
cur = cur.next;
}
}
}
/**
* 节点
*/
static class Emp {
public int id;
public String name;
public Emp next;
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}