java实现哈希表的增删改查

// 建立一个哈希表

class HashTab {
    private int size;
    // 建立一个链表数组
    EmpList[] empLists;

    public HashTab(int size) {
        this.size = size;
        // 构建整个数组
        empLists = new EmpList[size];

        // 对数组中的链表进行初始化
        for (int i = 0; i < size; i++) {
            empLists[i] = new EmpList();
        }

    }

    // 散列函数
    public int hashMethod(int id) {
        return id % size;

    }

    // 添加方法
    public void add(Emp emp) {
        // 通过散列函数返回的关键码值
        int i = hashMethod(emp.getId());
        empLists[i].add(emp);
    }

    // 删除方法
    public void remove(int id) {
        int i = hashMethod(id);
        empLists[i].remove(id);
    }
    
    //修改
    public void update(Emp emp){
    int i = hashMethod(emp.getId());
    empLists[i].update(emp);
        
    }

    // 遍历方法
    public void list() {
        for (int i = 0; i < size; i++) {
            empLists[i].list(i);
        }
    }

}

// 新建一个hash的链表
class EmpList {
    private Emp headEmp;

    // 添加
    public void add(Emp emp) {
        if (headEmp == null) {
            headEmp = emp;
            return;
        }
        Emp temp = headEmp;
        while (true) {
            if (temp.getNext() == null) {
                temp.setNext(emp);
                break;
            }
            temp = temp.getNext();

        }
    }

    // 删除
    public void remove(int id) {
        if (headEmp == null) {
            System.out.println("链表为空,找不到需要删除的雇员");
            return;
        }
        Emp temp = headEmp;
        while (true) {
            if (headEmp.getId() == id) {
                headEmp = headEmp.getNext();
                return;
            }
            if (temp.getNext() == null) {
                break;
            }
            if (temp.getNext().getId() == id) {
                temp.setNext(temp.getNext().getNext());
                return;
            } else {
                System.out.println("找不到需要删除的雇员");
            }
            temp = temp.getNext();

        }

    }

    // 修改
    public void update(Emp emp) {
        if (headEmp == null) {
            System.out.println("该链表为空");
            return;
        }
        Emp temp = headEmp;
        while (true) {
            if(temp.getId() ==emp.getId()){
                temp.setName(emp.getName());
                break;
            }
            if (temp.getNext() == null) {
                break;
            }

            temp = temp.getNext();

        }
    }

    // 遍历
    public void list(int id) {
        if (headEmp == null) {
            System.out.println("第 " + (id + 1) + " 链表为空");
            return;
        }
        Emp temp = headEmp;
        System.out.print("第 " + (id + 1) + " 链表的雇员为");
        while (true) {
            // 如果不为空就会最少有一个值
            System.out.print("-->" + temp.getId() + " " + temp.getName());
            if (temp.getNext() == null) {
                break;
            }
            temp = temp.getNext();
        }

        System.out.println();
    }

}

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值