牛搬家网企业网站排名,汽车4s网站设计,哪个协会要做网站建设啊,dedecms网站安装1.初识#x1f3b6;#x1f3b6;#x1f3b6; 它基本上是由一个数组和一个哈希函数组成的。哈希函数将每个键映射到数组的特定索引位置#xff0c;这个位置被称为哈希码。当我们需要查找一个键时#xff0c;哈希函数会计算其哈希码并立即返回结果#xff0c;因此我们可以…1.初识 它基本上是由一个数组和一个哈希函数组成的。哈希函数将每个键映射到数组的特定索引位置这个位置被称为哈希码。当我们需要查找一个键时哈希函数会计算其哈希码并立即返回结果因此我们可以直接访问该索引位置并读取或修改该键的值从而实现快速查找。 2.案例分析
使用哈希表存储人员信息 3.创建人员
/*** 每一位人员*/
class Emp {public int id;public String name;public Emp next;public Emp(int id, String name) {this.id id;this.name name;}Overridepublic String toString() {return Emp{ id id , name name \ };}
4.创建链表
/*** 链表*/
class EmpLinkList {private Emp head;//头节点/*** 添加员工*/public void add(Emp emp) {if (head null) {head emp;return;}Emp temp head;while (true) {if (temp.next null) {break;}temp temp.next;//指针后移}temp.next emp;}/*** 遍历链表*/public void list(int no) {if (head null) {System.out.println(第 (no 1) 条链表为空~);return;}Emp temp head;while (true) {System.out.print(第 (no 1) 链表 当前用户id: temp.id 姓名: temp.name);if (temp.next null) {break;}temp temp.next;//指针后移}System.out.println();}/*** 查找人员*/public Emp selectById(int id) {if (head null) {System.out.println(链表为空);return null;}Emp temp head;while (true) {if (temp.id id) {//找到break;}if (temp.next null) {//链表结尾temp null;break;}temp temp.next;}return temp;}/*** 删除*/public void delete(int id) {if (head null) {System.out.println(链表为空);return;}Emp temp head;while (true) {if (temp.next.id id) {//找到break;}if (temp.next null) {break;}temp temp.next;}temp.next temp.next.next;}5.创建哈希表
/*** 哈希表*/
class Hash {private EmpLinkList[] empLinkListArray;//定义个管理链表的数组private int size;//共有几条链表public Hash(int size) {this.size size;empLinkListArray new EmpLinkList[size];//初始化每一条链表for (int i 0; i size; i) {empLinkListArray[i] new EmpLinkList();}}/*** 添加人员*/public void add(Emp emp) {//根据员工id得到该员工应当添加到哪条链表int empLinkListNo hashFun(emp.id);//将emp加入到对应的链表中empLinkListArray[empLinkListNo].add(emp);}/*** 遍历所有链表*/public void list() {for (int i 0; i size; i) {empLinkListArray[i].list(i);}}/*** 查找人员*/public void select(int id) {//1.确定到哪条链表找int empLinkListNo hashFun(id);Emp emp empLinkListArray[empLinkListNo].selectById(id);if (emp ! null) {System.out.println(在第 (empLinkListNo 1) 条链表中找到 id id 的信息: emp);} else {System.out.println(未找到);}}/*** 删除人员*/public void delete(int id) {int empLinkListNo hashFun(id);empLinkListArray[empLinkListNo].delete(id);}/*** 编写散列函数取模*/public int hashFun(int id) {return id % size;}}
6.测试 public static void main(String[] args) {Hash hash new Hash(7);String key ;Scanner scanner new Scanner(System.in);while (true) {System.out.println(请输入:);System.out.println(add添加人员);System.out.println(list显示人员);System.out.println(select查找人员);System.out.println(delete删除人员);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);hash.add(emp);break;case list:hash.list();break;case select:System.out.println(请输入你要查找的id:);id scanner.nextInt();hash.select(id);break;case delete:System.out.println(请输入要删除的id);id scanner.nextInt();hash.delete(id);break;case exit:System.out.println(谢谢使用~);System.exit(0);}}}