数据结构和算法--8哈希表

哈希表介绍

A.题目:

有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,姓名,年龄,名字,住址),当输入该员工的id时,要求查找到该员工的所有信息。不使用数据库,速度越快越好。
添加时,保证按照id从低到高插入

B.思路:

 

C.代码

package com.offcn.hashTab;

import java.util.Scanner;

//哈希表
public class HashTableDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        HashTab hashTab = new HashTab(5);
        String key = "";
        while (true){
            System.out.println("add 添加雇员");
            System.out.println("list 遍历雇员");
            System.out.println("search 查找雇员");
            System.out.println("delete 删除雇员");
            System.out.println("exit 退出");
            key = sc.next();
            switch (key){
                case "add":
                    System.out.println("请输入雇员id");
                    int id = sc.nextInt();
                    System.out.println("请输入雇员姓名");
                    String name =sc.next();
                    Emp emp = new Emp(id,name);
                    hashTab.addInHash(emp);
                    break;
                case "list":
                    hashTab.listInHash();
                    break;
                case "search":
                    System.out.println("请输入id");
                    int id2 = sc.nextInt();
                    hashTab.searchInHash(id2);
                    break;
                case "delete":
                    System.out.println("请输入id");
                    int id3 = sc.nextInt();
                    hashTab.deleteInHash(id3);
                    break;
                case "exit":
                    System.exit(0);
                    break;
                    default:
                        break;
            }
        }

    }
}
//内部是链表的数组
class HashTab{
    private EmpLinkedList[] empLinkedListArray;
    private int size;
    public HashTab(int size){
        this.size = size;
        empLinkedListArray = new EmpLinkedList[size];
        //初始化每条链表
        for(int i = 0; i < size;i++){
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }
    //添加
    public void addInHash(Emp emp){
        int index = emp.getId() % size;
        empLinkedListArray[index].add(emp);
    }
    //遍历
    public void listInHash(){
        for(int i= 0;i < size;i++){
            empLinkedListArray[i].list(i);
        }
    }
    //查找
    public void searchInHash(int id){
        int index = id % size;
        Emp emp = empLinkedListArray[index].search(id);
        if (emp != null) {
            System.out.println("扎到了!在第"+(index+1)+"条链表中");
        }else{
            System.out.println("未找到");
        }
    }
    //删除
    public void deleteInHash(int id){
        int index = id % size;
        empLinkedListArray[index].delete(id);
    }
}
//链表
class EmpLinkedList{
    //头节点
    private Emp head;
    //往链表中添加雇员
    public void add(Emp emp){
        //链表为空,则emp就放在头节点
        if(head == null){
            head = emp;
            return;
        }
        //链表里有节点,emp放在最后
        Emp curEmp = head;
        while (true){
            //遍历到最后
            if(curEmp.next == null){
                curEmp.next = emp;
                break;
            }
            curEmp = curEmp.next;
        }
    }
    //遍历链表
    public void list(int i){
        if(head == null){
            System.out.println(""+(i+1)+"条链表为空");
            return;
        }
        System.out.print(""+(i+1)+"条链表");
        Emp curEmp = head;
        while (true){
            System.out.print("=>id "+curEmp.getId()+"name "+curEmp.getName());
            if(curEmp.next == null){
                break;
            }
            curEmp = curEmp.next;
        }
        System.out.println();
    }
    //查询
    public Emp search(int id){
        if(head == null){
            System.out.println("链表为空");
            return null;
        }
        Emp curEmp = head;
        boolean flag = true;
        while (true){
            //找到了
            if(curEmp.getId() == id){
                break;
            }
            //未找到
            if(curEmp == null){
                flag = false;
                break;
            }
            curEmp = curEmp.next;
        }
        if(flag){
            return curEmp;
        }else{
            return null;
        }
    }
    //删除
    public void delete(int id){
        if(head == null){
            System.out.println("空链表");
            return;
        }
        Emp curEmp = head;
        if(head.getId() == id){
            head = curEmp.next;
            System.out.println("删除成功");
            return;
        }
        while (true){
            if(curEmp.next.getId() == id){
                curEmp.next = curEmp.next.next;
                System.out.println("删除成功");
                break;
            }
            if(curEmp == null){
                System.out.println("不存在该雇员,无法删除");
                break;
            }
            curEmp = curEmp.next;
        }

    }
}
//雇员信息
class Emp{
    private int id;
    private String name;
    public Emp next;
    public int getId() {
        return id;
    }

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

    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 + '\'' +
                '}';
    }
}

。。

 

posted @ 2020-03-26 14:08  白白3535  阅读(127)  评论(0)    收藏  举报