JAVA-数据结构与算法-哈希表

1,169 阅读2分钟

写在前面

哈希表

  • 散列表,根据关键码值,进行数据访问,通过关键码值映射散列函数到表中一个位置来访问,存放数据的数组叫做散列表
  • 散列函数,就是决定数据放在哪条链表里的规则
  • 哈希表,管理多条链表
class HashTable {
    private EmployeeLinkedList[] employeeLinkedListArr;
    private int size; //共有多少条链表
    public HashTable(int size) {
        //初始化链表数组
        employeeLinkedListArr = new EmployeeLinkedList[size];
        this.size = size;
        //分别初始化每个链表
        for (int i = 0; i < size; i++) {
            employeeLinkedListArr[i] = new EmployeeLinkedList();
        }
    }
    //添加
    public void add(Employee employee) {
        //根据id,决定存到数组的哪条链表中
        int employeeLinkedListNo = hashFunction(employee.id);
        employeeLinkedListArr[employeeLinkedListNo].add(employee);
    }
    //顺序添加
    public void addByOrder(Employee employee) {
        //根据id,决定存到数组的哪条链表中
        int employeeLinkedListNo = hashFunction(employee.id);
        employeeLinkedListArr[employeeLinkedListNo].addByOrder(employee);
    }
    //遍历
    public void list() {
        for (EmployeeLinkedList employeeLinkedList : employeeLinkedListArr) {
            employeeLinkedList.list();
        }
    }
    //查找
    public void findEmployeeById(int id) {
        int employeeLinkedListNo = hashFunction(id);
        Employee employee = employeeLinkedListArr[employeeLinkedListNo].findEmployeeById(id);
        if (employee != null) {
            System.out.println(employee);
        } else {
            System.out.println("No find");
        }
    }
    //删除
    public void delEmployeeById(int id) {
        int employeeLinkedListNo = hashFunction(id);
        employeeLinkedListArr[employeeLinkedListNo].delEmployeeById(id);
    }
    //散列函数,判断id
    private int hashFunction(int id) {
        //取模法
        return id % size;
    }
}
  • 节点
class Employee {
    public int id;
    public String name;
    public Employee next;
    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Employee() {
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + ''' +
                '}';
    }
}
  • 链表,管理节点,不带节点头
class EmployeeLinkedList {
    //头指针,指向第一个employee
    private Employee head;

    //添加
    public void add(Employee employee) {
        //如果是第一个
        if (head == null) {
            head = employee;
            return;
        }
        //定位到最后
        Employee cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        //加入
        cur.next = employee;
    }
    //顺序添加
    public void addByOrder(Employee employee) {
        //如果是第一个
        if (head == null) {
            head = employee;
            return;
        }
        if (employee.id < head.id) {
            employee.next = head;
            head = employee;
            return;
        } else if (employee.id == head.id) {
            System.out.println("Exist");
            return;
        }
        //定位到最后
        Employee cur = head;
        while (cur != null) {
            if (cur.next == null) {
                cur.next = employee;
                break;
            }
            if (employee.id < cur.next.id) {
                employee.next = cur.next;
                cur.next = employee;
                break;
            } else if (employee.id == cur.next.id) {
                System.out.println("Exist");
                break;
            }
            cur = cur.next;
        }
    }
    //遍历
    public void list() {
        if (head == null) {
            System.out.println("List is empty.");
            return;
        }
        Employee cur = head;
        while (cur != null) {
            System.out.println(cur);
            cur = cur.next;
        }
        System.out.println("--------------");
    }

    public Employee findEmployeeById (int id) {
        //判断链表是否为空
        if (head == null) {
            System.out.println("Empty");
        }
        Employee cur = head;
        while (true) {
           if (cur.id == id) {
               break;
           }
           if (cur.next == null) {
               cur = null;
               break;
           }
           cur = cur.next;
        }
        return cur;
    }

    public void delEmployeeById (int id) {
        if (head == null) {
            System.out.println("Empty");
            return;
        }
        Employee cur = head;
        if (cur.id == id) {
            head = head.next;
            return;
        }
        while (true) {
            if (cur.next == null) {
                System.out.println("No find");
                break;
            }
            if (cur.next.id == id) {
                cur.next = cur.next.next;
                break;
            }
            cur = cur.next;
        }

    }
}