数据结构-哈希表

哈希表

散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。

在这里插入图片描述

题目:

有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址…),当输入该员工的 id 时, 要求查找到该员工的 所有信息,使用哈希表来完成。

代码实现:

package com.tian.hash;

import java.util.Scanner;

public class HashTableTest {
    public static void main(String[] args) {

        //创建一个hash表
        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:查找雇员");
            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();
                    //创建一个雇员
                    Emp emp = new Emp(id, name);
                    hashTab.add(emp);
                    break;
                case "list" :
                    hashTab.list();
                    break;
                case "find" :
                    System.out.println("请输入你要查找的id:");
                    id = scanner.nextInt();
                    hashTab.findEmp(id);
                    break;
                case "exit" :
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }
    }
}

//创建一个雇员
class Emp{
    public int id;
    public String name;
    public Emp next;

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

//创建一个EmpLinkedList,用于表示链表
class EmpLinkedList{
    //头指针head是直接指向第一个Emp
    private Emp head;

    /*
        添加雇员
        添加雇员时 id是自动增长的,即id是从小到大分配的
        因此直接将要添加的雇员添加到链表的最后即可
     */
    public void add(Emp emp){
        //如果添加的是第一个雇员
        if (head == null){
            head = emp;
            return;
        }
        //如果不是第一个雇员,则将该链表定位到最后在进行添加
        Emp curEmp = head;
        while (true){
            if (curEmp.next == null){  //如果到链表的最后
                break;
            }
            curEmp = curEmp.next;
        }
        //进行添加
        curEmp.next = emp;
    }

    //遍历链表,输出雇员的信息
    public void list(int no){
        if (head == null){  //链表为空
            System.out.println("第" + (no + 1) +"条链表为空");
            return;
        }

        System.out.print("第" + (no + 1) +"链表的信息为:");
        Emp curEmp = head;  //辅助指针add
        while (true){
            System.out.println("id = " + curEmp.id + " --- name = " + curEmp.name);
            if (curEmp.next == null){       //遍历到链表的最后
                break;
            }
            curEmp = curEmp.next;//指针后移,进行遍历
        }
    }

    /*
        根据id查找雇员
        如果找到,就返回Emp,如果没有找到,就返回null
     */
    public Emp findEmpById(int id) {
        //判断链表是否为空
        if (head == null){
            System.out.println("链表为空!");
            return null;
        }

        Emp curEmp = head;  //辅助指针
        while (true) {
            if (curEmp.id == id){   //找到
                break;
            }

            if (curEmp.next == null) {
                curEmp = null;
                break;
            }
            curEmp = curEmp.next;
        }
        return curEmp;
    }

}


//创建HashTab,用于管理多条链表
class HashTab{
    private EmpLinkedList[] empLinkedListsArray;
    private int size;   //表示一共有多少条链表

    public HashTab(int size) {
        this.size = size;
        empLinkedListsArray = new EmpLinkedList[size];
        //分别初始化每个链表
        for (int i = 0; i < size; i++) {
            empLinkedListsArray[i] = new EmpLinkedList();
        }
    }

    //添加雇员
    public void add(Emp emp){
        //根据雇员的id判断该放到哪条链表上
        int empLinkedListsNo = hashset(emp.id);
        //将emp添加到对应的链表中
        empLinkedListsArray[empLinkedListsNo].add(emp);
    }

    //遍历全部的链表,
    public void list(){
        for (int i = 0; i < size; i++) {
            empLinkedListsArray[i].list(i);
        }
    }

    //编写散列函数,
    public int hashset(int id){
        return id % size;
    }

    //根据id查找雇员
    public void findEmp(int id){
        //使用散列函数确定在哪条链表上
        int empLinkedNo = hashset(id);
        Emp emp = empLinkedListsArray[empLinkedNo].findEmpById(id);
        if (emp != null){   //找到
            System.out.println("找到id为" + id + "的雇员,其姓名为:" + emp.name);
        } else {
            System.out.println("没哟找到该雇员!");
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值