根据id查找某个员工之哈希表实现

  • 根据用户的id查找用户的信息,不能用到数据库,使用hash表的散 列方法
  • 哈希表是由 链表+数组实现的,一个哈希表有多条链表,一个链表中存放着多个员工的信息
  • 这里假设 插入的id值是按从小到大的输入的
//员工信息表
class Employee {
    public int id;
    public String name;
    public Employee next;//指向下一个 Employee实体

    public Employee(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;
    }
}
//员工链表
class EmpLikedList {

    private Employee head;// 头指针 直接指向第一个Employee对象
    //添加雇员  假设直接 在链表后面加

    public void addEmp(Employee employee) {
        //是第一雇员
        if (head == null) {
            head = employee;
            return;
        }

        Employee curEmp = head;
        while (true) {
            if (curEmp.next == null) {
                break;
            } else {
                curEmp = curEmp.next;
            }
        }
        curEmp.next = employee;
    }

    //遍历员工链表的信息
    public void printList(int i) {
        if (head == null) {
            System.out.println("链表" + i + "为空");
            return;
        }
        Employee currentEmp = head; //记录下标
        while (true) {
            System.out.print(i + "号链表" + "id:" + currentEmp.getId() + " " + "name:" + currentEmp.getName() + "==>");
            System.out.println(" ");
            if (currentEmp.next == null) {
                break;
            }
            currentEmp = currentEmp.next;
        }
    }

    //根据id 查找员工信息
    public Employee findEmp(int id) {
        Employee curEmp = head;
        if (head == null) {
            System.out.println("该链表为空");
            return null;
        }

        while (true) {
            if (curEmp.getId() == id) {
                break;
            }
            curEmp = curEmp.next;
        }
        return curEmp;
    }
}

//HashTable
class Hashtables {
    public int size;
    public EmpLikedList[] empLikedList;

    //初始化
    public Hashtables(int size) {
        this.size = size;
        empLikedList = new EmpLikedList[size];//hash表的初始化
        //!!! Hash表中包括链表,链表也要初始化
        for (int i = 0; i < size; i++) {
            empLikedList[i] = new EmpLikedList();
        }
    }

    //根据id 判断 该元素在那个list链表中
    public int idFun(int id) {
        return id % size;
    }

    //添加Emp 我们只看的见HashTable 添加元素 也是在HashTable中添加
    public void addEmployes(Employee employee) {
        int numLocation = idFun(employee.id);
        empLikedList[numLocation].addEmp(employee);
    }

    //遍历所有链表
    public void traverse() {
        for (int i = 0; i < size; i++) {
            empLikedList[i].printList(i);
        }
    }

    //查找员工
    public void findEmp(int id) {
        int location = idFun(id);
        Employee employee = empLikedList[location].findEmp(id);
        if (employee == null) {
            System.out.println("没有该员工");
            return;
        } else {
            System.out.println("id:" + id + "name=" + " " + employee.getName());
        }

    }
}片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值