数据结构(六)---哈希表

哈系表

员工类

//表示一个雇员
class Employee {
    public int id;
    public String name;
    public Employee next;//next默认为null

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

员工链表

//创建EmpLinkedList,表示链表
class EmpLinkedList {
    //头指针,执行第一个Emp,因此我们这个链表的head,是直接指向第一个Emp
    private Employee head;//默认null,头指针

    //添加雇员到链表
    //当添加雇员时,id是自增长,就是id的分配总是从小到大,把雇员直接加到本链表
    public void add(Employee employee) {
        //如果是添加第一个雇员
        if (head == null) {
            head = employee;
            return;
        }
        //如果不是第一个雇员,就使用一个辅助的指针,帮助定位到最后
        Employee curEmp = head;
        while (true) {
            if (curEmp.next == null) {//如果到了链表最后
                break;
            }
            curEmp = curEmp.next;//后移
        }
        //退出时直接把employee加到最后就行
        curEmp.next = employee;
    }

    //遍历链表的雇员信息
    public void list(int no) {
        if (head == null) {//说明链表为空
            System.out.println("第" + (no + 1) + "条链表为空");
            return;
        }
        System.out.print("第" + (no + 1) + "条链表的信息为:");
        Employee curEmp = head;//辅助指针
        while (true) {
            System.out.printf(" => id=%d name=%s\t", curEmp.id, curEmp.name);
            if (curEmp.next == null) {//说明curEmp已经是最后结点
                break;
            }
            curEmp = curEmp.next;//后移,然后遍历
        }
        System.out.println();
    }

    //根据id查找雇员
    //如果查找到雇员,就返回信息,如果没找到,就返回null
    public Employee findEmpById(int id) {
        //判断链表是否为空
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        //辅助指针
        Employee curEmp = head;
        while (true) {
            if (curEmp.id == id) {//找到了
                break;//这时curEmp就指向要查找的雇员
            }
            if (curEmp.next == null) {//说明遍历当前链表没有找到该雇员
                curEmp = null;
            }
            curEmp = curEmp.next;
        }
        return curEmp;
    }
}

创建哈希表,管理多条链表

class HashTab {
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    //构造器
    public HashTab(int size) {
        this.size = size;
        //初始化empLinkedListArray
        empLinkedListArray = new EmpLinkedList[size];
        //留一个坑,真的可以用了么?
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }

    }

    //添加雇员
    public void add(Employee employee) {
        //根据员工id,得到这个员工应当添加到哪条链接
        int empLinkedListNO = hashFun(employee.id);
        //把employee添加到对应的链表中
        empLinkedListArray[empLinkedListNO].add(employee);
    }

    //遍历所有的链表,遍历hashtab
    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list(i);
        }
    }

    //根据输入的id,查找雇员
    public int findEmpById(int id) {
        //使用散列函数确定到哪条链表查找
        int empLinkedListNO = hashFun(id);
        Employee employee = empLinkedListArray[empLinkedListNO].findEmpById(id);
        if (employee != null) {//找到
            System.out.printf("在第%d条链表中找到雇员id=%d\n", empLinkedListNO + 1, id);
        } else {
            System.out.println("没有找到该雇员");
        }
        return id % size;
    }

    //编写散列函数,使用一个简单的取膜法
    public int hashFun(int id) {
        return id % size;
    }
}

测试类

/**
 * part06_哈希表(散列):
 * 一个实际需求,goole公司的一个上机题
 * 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住地...)
 * 当输入该员工的id时,要求查找到该员工的所有信息
 * <p>
 * 要求:不使用数据库,尽量节省内存,速度也快越好=>考虑用哈希表(散列)
 * <p>
 * 思考一下:HashTable,HashMap,TreeSet 这些java里的集合,底层是怎么实现的
 * <p>
 * 我们自己来写一个哈希表
 * <p>
 * part06_哈希表:
 * 也叫散列表,根据关键码值(key,value)而直接进行访问的数据结构
 * 通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度
 * <p>
 * hashtable结构为:
 * 数组+part03_链表
 * 数组+二叉树(hashmap为:数组+part03_链表+红黑树)
 * <p>
 * 计算方法:
 * 首先,使用哈希函数,value%length=k,这个k就是数组的索引,把数的key放到数组这个索引位置
 * 然后,把value放在数组后面的链表里
 * <p>
 * 分析这道题:
 * 把id当做key值,放在数组里面,每个人的id值都是唯一的
 * 其他具体信息当做value值,放在数组后面的链表里
 * 数组的索引为id%arr.length
 * 链表的头指针指向链表的第一个雇员
 * 写的时候,要写一个员工类存放信息,一个员工的链表包含员工类信息,最后整合成一个哈希表类包含链表和数组
 */
public class HashTable {
    public static void main(String[] args) {
        //创建一个哈希表
        HashTab hashTab = new HashTab(7);
        //写一个简单的菜单
        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("add:添加雇员");
            System.out.println("list:遍历雇员");
            System.out.println("find:根据id查找雇员");
            System.out.println("exit:退出系统");

            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.println("输入id");
                    int id = scanner.nextInt();
                    System.out.println("输入名字");
                    String name = scanner.next();

                    //创建一个雇员
                    Employee employee = new Employee(id, name);
                    hashTab.add(employee);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入要查找的id");
                    id=scanner.nextInt();
                    hashTab.findEmpById(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }

    }

}
发布了41 篇原创文章 · 获赞 5 · 访问量 648

猜你喜欢

转载自blog.csdn.net/weixin_44823875/article/details/104934761