当前位置:网站首页>哈希表 详细讲解
哈希表 详细讲解
2022-07-27 11:00:00 【舒克日记】
1、哈希表的基本介绍
散列表(Hash table,也叫哈希表)是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列数存放记录的数组叫做散列表
2、哈希表(散列-Google)上机题
1)看一个实际需求,google公司的一个上机题:
2)有一个公司,当有新的员工来报道时,要求将该员工的信息加入d,性别,年龄,住址),当输入该员工的id时,要求查找到该员工的所有信息
3)要求:不使用数据库,尽量节省内存,速度越快越好=>哈希表(散列)
3、哈希表(散列-Google)上机题思路分析

4、代码实现
package com.qf.hash;
import java.util.Scanner;
public class HashTabDemo {
public static void main(String[] args) {
HashTab hashTab=new HashTab(8);
boolean loop=true;
String sc="";
while (loop){
System.out.println("add 添加员工");
System.out.println("list 展示员工");
System.out.println("find 查找员工");
System.out.println("exit 退出");
Scanner systemPut=new Scanner(System.in);
sc=systemPut.next();
switch (sc){
case "add":
System.out.println("请输入员工编号:");
Scanner addNum=new Scanner(System.in);
int id = addNum.nextInt();
System.out.println("请输入员工名字:");
Scanner nameScan=new Scanner(System.in);
String name = nameScan.next();
Emp emp=new Emp(id,name);
hashTab.add(emp);
break;
case "list":
hashTab.list();
break;
case "find":
System.out.println("请输入员工编号:");
Scanner find=new Scanner(System.in);
int findId = find.nextInt();
hashTab.find(findId);
break;
case "exit":
loop=false;
System.out.println("退出循环");
break;
default :
break;
}
}
}
}
class HashTab{
public EmpLinkedList[] linkedListArray;
public int maxSize;
public HashTab(int maxSize){
this.maxSize=maxSize;
linkedListArray=new EmpLinkedList[maxSize];
for (int i = 0; i < linkedListArray.length; i++) {
linkedListArray[i]=new EmpLinkedList();
}
}
public void add(Emp emp){
int id=emp.id;
int location=id%maxSize;
EmpLinkedList linkedList=linkedListArray[location];
linkedList.add(emp);
}
public void list(){
for (int i = 0; i < linkedListArray.length; i++) {
linkedListArray[i].list(i);
}
}
public void find(int id){
int location=id%maxSize;
EmpLinkedList linkedList=linkedListArray[location];
linkedList.find(id);
}
}
class EmpLinkedList{
public Emp head;
public EmpLinkedList(){
}
public void add(Emp emp){
if (head==null){
head=emp;
}else{
Emp temp=head;
while (true){
if (temp.next==null){
break;
}
temp=temp.next;
}
temp.next=emp;
}
}
public void list(int i){
if (head==null){
System.out.println("第"+i+"条链表的长度为空");
}else {
Emp temp=head;
while (true){
System.out.println("第"+i+"条链表员工id为:"+temp.id+"名字为:"+temp.name);
if (temp.next==null){
break;
}
temp=temp.next;
}
}
}
public void find(int id){
if (head==null){
System.out.println("链表长度为空,未能找到员工");
}else{
Emp temp=head;
boolean isFind=false;
while (true){
if (temp.id==id){
isFind=true;
System.out.println("员工的名字为:"+temp.name);
break;
}
if (temp.next==null){
break;
}
temp=temp.next;
}
if (!isFind){
System.out.println("未能找到员工");
}
}
}
}
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 + '\'' +
'}';
}
}
边栏推荐
- 容斥原理 AcWing 890. 能被整除的数
- Maker Hongmeng application development training notes 02
- torch‘ has no attribute ‘inference_mode‘
- WGet warning: unable to verify
- LeetCode 01: T1. 两数之和 ; T1108. IP 地址无效化 ; T344. 反转字符串
- C custom set
- 美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生
- Vscode establishes automatic search of header files under non engineering directories
- Digital triangle model acwing 1018. Minimum toll
- C programming language (2nd Edition) -- Reading Notes -- 1.5.2
猜你喜欢

JUC框架 从Runnable到Callable到FutureTask 使用浅析

When std:: bind meets this

Moveit2 -- 2. Quick start of moveit in rviz

Solutions to errors in tensorflow operation

Find the combination number acwing 886. find the combination number II

VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景

PWM的原理和PWM波的产生

Win10 vscode code code format setting and remote breakpoint debugging

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

82.(cesium之家)cesium点在3d模型上运动
随机推荐
[unity entry program] creator kitfps: first person shooting 3D game
第12章 泛型
EfficientNet
微商的差商近似
Chinese remainder theorem acwing 204. strange way of expressing integers
开源flink有写holo的Datastream connector或者flink sql conn
局域网SDN硬核技术内幕 23 展望未来——RDMA(上)
求组合数 AcWing 888. 求组合数 IV
Solutions to errors in tensorflow operation
Newton-Raphson迭代法
Game theory acwing 894. Split Nim game
为什么TCP三次握手的时候ACK=Seq+1
你真的会写二分查找吗——变种二分查找
第10章 枚举类与注解
Maker harmony OS application development training notes 01
What is private traffic?
makefile模板
C programming language (2nd Edition) -- Reading Notes -- 1.5.2
微机和单片机的区别
Quantitative industry knowledge summary
