算法与数据结构-07-手写类HashTable

package day05;


import java.sql.SQLOutput;
import java.util.Scanner;

/**
 * 哈希表代码实现
 */
public class HashTab {

    public static void main(String[] args) {
        System.out.println("-----------功能-----------");
        System.out.println("查询所有员工:findAll");
        System.out.println("查询指定ID员工:findOne");
        System.out.println("添加员工:add");
        System.out.println("删除员工:delete");
        System.out.println("清空员工:clear");
        System.out.println("--------------------------");
        while (true) {
            System.out.print("请输入需要执行功能:");
            String handle = new Scanner(System.in).next();
            if (handle.equals("add")) {
                System.out.print("请输入用户ID:");
                int id = new Scanner(System.in).nextInt();
                System.out.print("请输入用户姓名:");
                String name = new Scanner(System.in).next();
                Emp emp = new Emp(id, name);
                add(emp);
            } else if (handle.equals("findAll")) {
                findAll();
            } else if (handle.equals("findOne")) {
                System.out.print("请输入要查询员工ID:");
                int id = new Scanner(System.in).nextInt();
                findOne(id);
            } else if (handle.equals("delete")) {
                System.out.print("请输入要删除员工ID:");
                int id = new Scanner(System.in).nextInt();
                delete(id);
            } else if (handle.equals("clear")) {
                System.out.println("正在清理所有员工信息...");
                clearEmp();
            } else if (handle.equals("exit")) {
                System.out.println("退出功能...");
                break;
            }
        }
    }

    public static final int SIZE = 7;

    public static EmployeeList[] employeeLists = new EmployeeList[SIZE];

    static {
        for (int i = 0; i < employeeLists.length; i++) {
            employeeLists[i] = new EmployeeList();
        }
    }


    private static void delete(int id) {
        int hashIndex = hashIndex(id);
        EmployeeList employeeList = employeeLists[hashIndex];
        employeeList.delete(id);
    }


    private static void findOne(int id) {
        int hashIndex = hashIndex(id);
        EmployeeList employeeList = employeeLists[hashIndex];
        employeeList.findOne(id);
    }

    public static void findAll() {
        for (int i = 0; i < employeeLists.length; i++) {
            System.out.print("第" + (i + 1) + "条链表:");
            employeeLists[i].findAll();
            System.out.println();
        }
    }

    public static void add(Emp emp) {
        if (emp == null) {
            throw new IllegalArgumentException("添加对象不能为空");
        }
        int hashIndex = hashIndex(emp.getId());
        EmployeeList employeeList = employeeLists[hashIndex];
        employeeList.add(emp);
    }

    private static void clearEmp() {
        for (int i = 0; i < employeeLists.length; i++) {
            EmployeeList employeeList = employeeLists[i];
            employeeList.claerEmp();
            System.out.println("清理所有员工结束...");
        }
    }

    /**
     * 对操作对象的Id进行hash(此处取余)
     *
     * @param id
     * @return
     */
    public static int hashIndex(int id) {
        return id % SIZE;
    }


}

/**
 * 员工链表类
 */
class EmployeeList {

    public Emp head = new Emp();

    /**
     * 添加用户
     *
     * @param emp
     */
    public void add(Emp emp) {
        Emp cur = head;
        while (true) {
            if (cur.next == null) {
                cur.next = emp;
                System.out.println(emp.getName() + "添加成功");
                break;
            }
            cur = cur.next;
        }
    }

    /**
     * 打印该链表所有用户
     */
    public void findAll() {
        Emp cur = head.next;
        while (true) {
            if (cur == null) {
                System.out.print("null");
                break;
            }
            System.out.print("[" + cur.getId() + "," + cur.getName() + "]->");
            cur = cur.next;
        }
    }

    /**
     * 根据员工ID查询
     *
     * @param id
     */
    public void findOne(int id) {
        Emp cur = head.next;
        while (true) {
            if (cur == null) {
                System.out.println(id + "对应的员工不存在.");
                break;
            }
            if (cur.getId() == id) {
                System.out.println("[" + cur.getId() + "," + cur.getName() + "]");
                break;
            } else {
                cur = cur.next;
            }
        }
    }

    /**
     * 删除员工
     *
     * @param id
     */
    public void delete(int id) {
        Emp cur = head;
        while (true) {
            if (cur.next == null) {
                System.out.println(id + "对应的员工不存在.");
                break;
            }
            if (cur.next.getId() == id) {
                cur.next = cur.next.next;
                break;
            } else {
                cur = cur.next;
            }
        }
    }


    public void claerEmp() {
        if (head.next == null) {
            return;
        } else {
            head.next = null;
        }
    }

}

/**
 * 员工类
 */
class Emp {
    private int id;
    private String name;
    public Emp next;

    public Emp() {
    }

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章