哈希表(散列)-Google上机题
看一个实际需求,google公司的一个上机题:
有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址…),当输入该员工的id时,要求查找到该员工的 所有信息.
要求: 不使用数据库,尽量节省内存,速度越快越好=>哈希表(散列)
哈希表基本介绍
散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
哈希表内存布局图:
哈希表应用实例
google公司的一个上机题:
有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址…),当输入该员工的id时,要求查找到该员工的 所有信息.
要求:
- 不使用数据库,速度越快越好=>哈希表(散列)
- 添加时,保证按照id从低到高插入
- 使用链表来实现哈希表, 该链表不带表头[即: 链表的第一个结点就存放雇员信息]
- 思路分析并画出示意图
代码实现
package hashTable;
import java.util.Scanner;
public class HashTableDemo {
public static void main(String[] args) {
// 创建哈希表
HashTable hashTable = new HashTable(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("del:删除雇员");
System.out.println("edit:编辑雇员");
System.out.println("exit:退出系统");
key = scanner.next();
switch (key) {
case "add":
System.out.print("输入id:");
int addId = scanner.nextInt();
System.out.print("输入名字:");
String name = scanner.next();
// 创建雇员
Emp emp = new Emp(addId, name);
hashTable.add(emp);
break;
case "list":
hashTable.list();
break;
case "find":
System.out.print("输入id:");
int findId = scanner.nextInt();
hashTable.findEmpById(findId);
break;
case "del":
System.out.print("输入id:");
int removeId = scanner.nextInt();
hashTable.removeEmpById(removeId);
break;
case "edit":
System.out.print("输入id:");
int editId = scanner.nextInt();
System.out.print("输入名字:");
String editName = scanner.next();
hashTable.editEmpById(editId,editName);
break;
case "exit":
scanner.close();
System.exit(0);
break;
}
}
}
}
// 创建哈希Table 管理多条链表
class HashTable {
private EmpLinkedList[] empLinkedListArr;
private int size; // 表示共有多少条链表
// 构造器
public HashTable(int size) {
// 初始化empLinkedListArr
empLinkedListArr = new EmpLinkedList[size];
this.size = size;
// 这是不要忘记初始化每个链表
for (int i = 0; i < size; i++) {
empLinkedListArr[i] = new EmpLinkedList();
}
}
// 添加雇员
public void add(Emp emp) {
// 根据员工的ID,得到该员工应该插入到哪条链表
int empLinkedListNO = hashFun(emp.id);
// 将emp添加到对应的链表中
empLinkedListArr[empLinkedListNO].add(emp);
}
// 遍历所有的链表(遍历哈希表)
public void list() {
for (int i = 0; i < size; i++) {
empLinkedListArr[i].list(i);
}
}
// 编写一个散列函数,使用简单取模法
public int hashFun(int id) {
return id % size;
}
// 根据输入的id查找雇员
public void findEmpById(int id) {
// 根据散列函数确定去哪条链表查找
int empLinkedListNo = hashFun(id);
Emp emp = empLinkedListArr[empLinkedListNo].findEmpById(id);
if (emp != null) {
System.out.println("在第" + empLinkedListNo + "条链表中找到该雇员,姓名为:" + emp.name);
} else {
System.out.println("在哈希表中没有找到该雇员!");
}
}
// 根据id修改雇员名称
public void editEmpById(int id,String name){
int empLinkedListNo = hashFun(id);
empLinkedListArr[empLinkedListNo].editTmpById(id,name);
}
public void removeEmpById(int id) {
int empLinkedListNo = hashFun(id);
empLinkedListArr[empLinkedListNo].removeEmpById(id);
}
}
//创建一个EmpLinkedList,表示链表
class EmpLinkedList {
// 头指针 指向第一个雇员(Emp),因此我们这个链表的head是直接指向第一个Emp的
private Emp head; // 默认为空
// 添加雇员到链表
// 说明
// 1. 假定,当添加雇员时候,ID是自增长的,即ID的分配总是从小到大
// 2. 因此,我们将该雇员直接加入到本链表的最后即可
public void add(Emp emp) {
// 如果是添加第一个雇员
if (head == null) {
head = emp;
return;
}
// 如果不是第一个 使用一个辅助指针 绑定定位到最后
Emp temp = head;
while (temp.next != null) {
// 如果temp的下一个不为空,则不断后移
temp = temp.next;
}
// 退出时直接将emp加到链表最后即可
temp.next = emp;
}
// 遍历链表的雇员信息
public void list(int no) {
if (head == null) {
System.out.println("第(" + (no + 1) + ")条链表为空没有雇员");
return;
}
System.out.print("第(" + (no + 1) + ")条链表雇员的信息为:");
Emp temp = head;
while (temp != null) {
System.out.printf("编号%d)的姓名为:%s ", temp.id, temp.name);
temp = temp.next;
}
System.out.println();
}
// 根据id查找雇员
// 如果查找到,返回Emp,如果没有找到,返回null
public Emp findEmpById(int id) {
// 判断链表是否为空
if (head == null) {
System.out.println("链表为空!");
return null;
}
Emp temp = head;
while (true) {
if (temp.id == id) {
// 找到
break; // 此时temp指向要查找的雇员
}
if (temp.next == null) {
// 遍历当前链表没有找到该雇员
temp = null;
break;
}
temp = temp.next;
}
return temp;
}
// 根据id删除雇员
public void removeEmpById(int id) {
// 判断链表是否为空
if (head == null) {
System.out.println("没有找到该节点!");
return;
}
// 如果头节点就是要删除的值
if (head.id == id) {
System.out.println("删除结点成功");
if (head.next != null) {
head = head.next;
return;
}
head = null;
return;
}
if (head.next == null) {
System.out.println("没有找到该节点!");
return;
}
// 头节点不是要删除的值
boolean flag = false;
Emp temp = head;
while (true) {
if (temp.next.id == id) {
flag = true;
break;
}
if (temp.next.next == null) {
break;
}
temp = temp.next;
}
if (flag) {
System.out.println("删除节点成功!");
temp.next = temp.next.next;
}
}
// 根据id修改雇员名称
public void editTmpById(int id,String name){
if(head == null){
System.out.println("链表为空");
}
Emp temp = head;
while(temp!=null){
if(temp.id == id){
temp.name = name;
System.out.println("名称修改成功!");
return;
}
}
System.out.println("没有找到该节点!");
}
}
// 表示一个雇员
class Emp {
public int id;
public String name;
public Emp next; // next 默认为空
public Emp(int id, String name) {
super();
this.id = id;
this.name = name;
}
}