Java代码实现哈希表(google 公司的上机题)

2025-05-29 0 38

1 哈希表(散列)-Google 上机题

1) 看一个实际需求,google 公司的一个上机题:
2) 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的 id 时,要求查
找到该员工的 所有信息.
3) 要求: 不使用数据库,尽量节省内存,速度越快越好=>哈希表(散列)

2 哈希表的基本介绍

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

Java代码实现哈希表(google 公司的上机题)

3. google 公司的一个上机题:

有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址..),当输入该员工的 id 时,
要求查找到该员工的 所有信息.
要求:
  1) 不使用数据库,,速度越快越好=>哈希表(散列)
  2) 添加时,保证按照 id 从低到高插入
  3) 使用链表来实现哈希表, 该链表不带表头[即: 链表的第一个结点就存放雇员信息]
  4) 思路分析并画出示意图

Java代码实现哈希表(google 公司的上机题)

  5) 代码实现

  1. public class HashTableDemo {
  2. public static void main(String[] args) {
  3. HashTable hashTable = new HashTable(7);
  4. String key = "";
  5. Scanner scanner = new Scanner(System.in);
  6. while(true) {
  7. System.out.println("add:添加雇员");
  8. System.out.println("list:查看雇员");
  9. System.out.println("find:查找雇员");
  10. System.out.println("del:删除雇员");
  11. System.out.println("exit:退出");
  12. key = scanner.next();
  13. switch (key) {
  14. case "add":
  15. System.out.println("请输入id:");
  16. int id = scanner.nextInt();
  17. System.out.println("请输入名字:");
  18. String name = scanner.next();
  19. Emp emp = new Emp(id, name);
  20. hashTable.add(emp);
  21. break;
  22. case "list":
  23. hashTable.list();
  24. break;
  25. case "find":
  26. System.out.println("请输入id:");
  27. int id2 = scanner.nextInt();
  28. hashTable.findEmpById(id2);
  29. break;
  30. case "del":
  31. System.out.println("请输入id:");
  32. int id3 = scanner.nextInt();
  33. hashTable.del(id3);
  34. break;
  35. case "exit":
  36. System.exit(10);
  37. default:
  38. break;
  39. }
  40. }
  41. }
  42. }
  1. // emp
  2. class Emp{
  3. public int id;
  4. public String name;
  5. public Emp next;
  6. public Emp(int id, String name) {
  7. super();
  8. this.id = id;
  9. this.name = name;
  10. }
  11. }
  1. // EmpLinkedList
  2. class EmpLinkedList{
  3. // 头指针,执行第一个Emp,因此我们这个链表的head,是直接指向第一个Emp
  4. private Emp head;
  5. // id是自增长的
  6. public void add(Emp emp) {
  7. // 如果是添加一个雇员
  8. if(head == null) {
  9. head = emp;
  10. return;
  11. }
  12. // 如果不是第一个
  13. Emp curEmp = head;
  14. while(true) {
  15. if(curEmp.next == null) {
  16. break;
  17. }
  18. curEmp = curEmp.next;
  19. }
  20. curEmp.next = emp;
  21. }
  22. public void list(int no) {
  23. if(head == null) {
  24. System.out.println("第" + (no+1) + "条链表为空!");
  25. return;
  26. }
  27. System.out.println("第" + (no+1) + "条链表信息为:");
  28. Emp curEmp = head;
  29. while(true) {
  30. System.out.printf("=> id=%d name=%s\\t",curEmp.id,curEmp.name);
  31. if(curEmp.next == null) {
  32. break;
  33. }
  34. curEmp = curEmp.next;
  35. }
  36. System.out.println();
  37. }
  38. // 根据id查找雇员
  39. public Emp findEmpByid(int id) {
  40. if(head == null) {
  41. System.out.println("链表为空");
  42. return null;
  43. }
  44. Emp curEmp = head;
  45. while(true) {
  46. if(curEmp.id == id) {
  47. break;
  48. }
  49. if(curEmp.next == null) {
  50. System.out.println("遍历完了,没有找到!");
  51. curEmp = null;
  52. break;
  53. }
  54. curEmp = curEmp.next;
  55. }
  56. return curEmp;
  57. }
  58. // 根据id进行删除
  59. public boolean del(int id) {
  60. boolean flag = false;
  61. if(head == null) {
  62. System.out.println("当前链表为空!");
  63. return flag;
  64. }
  65. if(head.id == id) {
  66. head = null;
  67. flag = true;
  68. return flag;
  69. }
  70. Emp curEmp = head;
  71. while(true) {
  72. // 找到了改雇员
  73. if(curEmp.next.id == id) {
  74. curEmp.next = curEmp.next.next;
  75. curEmp.next = null;
  76. return (flag == false);
  77. }
  78. // 没有找到
  79. if(curEmp.next == null) {
  80. System.out.println("没有找改雇员!");
  81. curEmp = null;
  82. return flag;
  83. }
  84. curEmp = curEmp.next;
  85. }
  86. }
  87. }
  1. // 哈希表
  2. class HashTable{
  3. private EmpLinkedList[] empLinkedListArr;
  4. private int size;
  5. public HashTable(int size) {
  6. super();
  7. this.size = size;
  8. empLinkedListArr = new EmpLinkedList[size];
  9. for(int i = 0; i < size; i++){
  10. empLinkedListArr[i] = new EmpLinkedList();
  11. }
  12. }
  13. // 添加雇员
  14. public void add(Emp emp) {
  15. // 根据员工的id得到改员工应该添加到哪条链表
  16. int empLinkedListNo = hashFun(emp.id);
  17. // 将emp添加到对应的链表中
  18. empLinkedListArr[empLinkedListNo].add(emp);
  19. }
  20. public void list() {
  21. for (int i = 0; i < empLinkedListArr.length; i++) {
  22. empLinkedListArr[i].list(i);
  23. }
  24. }
  25. public void findEmpById(int id) {
  26. int empLinkedListNo = hashFun(id);
  27. Emp emp = empLinkedListArr[empLinkedListNo].findEmpByid(id);
  28. if(emp != null) {
  29. System.out.println("在第" + (empLinkedListNo+1) + "条链表中找到id = " + id + "雇员");
  30. } else {
  31. System.out.println("在哈希表中没有找到");
  32. }
  33. }
  34. public void del(int id) {
  35. int empLinkedListNo = hashFun(id);
  36. boolean flag = empLinkedListArr[empLinkedListNo].del(id);
  37. if(flag == true) {
  38. System.out.println("在第" + (empLinkedListNo+1) + "条链表中删除了id = " + id + "雇员");
  39. } else {
  40. System.out.println("在哈希表中没有找到");
  41. }
  42. }
  43. public int hashFun(int id) {
  44. return id %size;
  45. }
  46. }

Java代码实现哈希表(google 公司的上机题)

Java代码实现哈希表(google 公司的上机题)

注意:不要把链表的第一个节点(头节点)删除了,不然整条链表没了。(还可以改良)

思考:如果 id 不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?

到此这篇关于Java 哈希表(google 公司的上机题)的文章就介绍到这了,更多相关java哈希表内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://www.cnblogs.com/linzm14/archive/2021/03/07/14495883.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java代码实现哈希表(google 公司的上机题) https://www.kuaiidc.com/108401.html

相关文章

发表评论
暂无评论