首頁>Club>
2
回覆列表
  • 1 # 使用者4510654793905

    Hash程式碼的實現

    Employee實體類:


    package com.test;

    /**

    * @author lizhangyu

    * @date 2021/3/6 12:23

    */

    public class Employee {

    public int id;

    public String name;

    public Employee next;

    public Employee(int id, String name) {

    this.id = id;

    this.name = name;

    }

    }

    EmpLinkedList實體類

    package com.test;

    /**

    * @author lizhangyu

    * @date 2021/3/6 12:26

    */

    public class EmpLinkedList {

    private Employee head;

    /**

    * 新增

    * @param employee

    */

    public void add(Employee employee) {

    if (head == null) {

    head = employee;

    return;

    }

    Employee cur = head;

    while (true) {

    if (cur.next == null) {

    break;

    }

    cur = cur.next;

    }

    cur.next = employee;

    }

    /**

    * 遍歷連結串列

    * @param no

    */

    public void list(int no) {

    if (head == null) {

    System.out.println("第" + (no + 1) + "條連結串列為空" );

    return;

    }

    System.out.print("第" + (no + 1) + "條連結串列資訊為");

    Employee cur = head;

    while(true) {

    System.out.printf("=> id=%d name=%s\t",cur.id, cur.name);

    if (cur.next == null) {

    break;

    }

    cur = cur.next;

    }

    System.out.println();

    }

    /**

    * 查詢

    * @param id

    * @return

    */

    public Employee find(int id) {

    if (head == null) {

    System.out.println("連結串列為空");

    return null;

    }

    Employee cur = head;

    while (true) {

    if (cur.id == id) {

    break;

    }

    if (cur.next == null) {

    cur = null;

    break;

    }

    cur = cur.next;

    }

    return cur;

    }

    }

    HashTable實體類:

    package com.test;

    /**

    * @author lizhangyu

    * @date 2021/3/6 12:06

    */

    public class HashTable {

    private int size;

    private EmpLinkedList[] empLinkedListArray;

    public HashTable(int size) {

    this.size = size;

    this.empLinkedListArray = new EmpLinkedList[size];

    //初始化連結串列

    for (int i = 0; i < size ; i++) {

    empLinkedListArray[i] = new EmpLinkedList();

    }

    }

    /**

    * 查詢

    * @param id

    */

    public void find(int id) {

    int empLinkedListNO = hashFun(id);

    Employee employee = empLinkedListArray[empLinkedListNO].find(id);

    if (employee != null) {

    System.out.printf("在第%d條連結串列中找到僱員id = %d\n", (empLinkedListNO + 1), id);

    }else {

    System.out.println("在雜湊表中中找到僱員");

    }

    }

    /**

    * 遍歷

    */

    public void list() {

    for (int i = 0; i < size; i++) {

    empLinkedListArray[i].list(i);

    }

    }

    /**

    * 新增

    * @param employee

    */

    public void add(Employee employee) {

    int employeeNo = hashFun(employee.id);

    empLinkedListArray[employeeNo].add(employee);

    }

    /**

    * 取模運算

    * @param id

    * @return

    */

    public int hashFun(int id) {

    return id % size;

    }

    }

    HashTableDemo測試類

    package com.test;

    import java.util.Scanner;

    /**

    * @author lizhangyu

    * @date 2021/3/6 12:45

    */

    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("exit: 退出系統");

    key = scanner.next();

    switch (key) {

    case "add" :

    System.out.println("請輸入員工id");

    int id = scanner.nextInt();

    System.out.println("請輸入員工名字");

    String name = scanner.next();

    Employee employee = new Employee(id, name);

    hashTable.add(employee);

    break;

    case "find" :

    System.out.println("請輸入員工id");

    id = scanner.nextInt();

    hashTable.find(id);

    break;

    case "list" :

    hashTable.list();

    break;

    case "exit" :

    scanner.close();

    System.exit(0);

    default:

    break;

    }

    }

    }

    }

  • 中秋節和大豐收的關聯?
  • 世上沒有絕對的好人和壞人的說說?