• 0086 哈希表


     

     

     

     

     

     

    import java.util.Scanner;

    /*
     * 哈希表(Hash Table)
     * 也叫散列表,是根据关键码值(Key Value)而直接进行访问的数据结构。
     * 即通过把关键码值映射到一个表中一个位置来访问记录,加快查找速度。
     * 这个映射函数叫散列函数,存放记录的数组叫做散列表
     * 
     * 上机题:
     * 有一个公司,当有新员工报道时,要求将该员工的信息加入(id,名字)
     * 当输入该员工id时,要求查找该员工的所有信息
     * 要求:不使用数据库,速度越快越好==>哈希表
     * 
     * 1.使用链表来实现哈希表,不带头结点,即第一个结点就存放雇员信息
     * 2.实现增删改查
     */
    public class HashTable_ {
        public static void main(String[] args) {
            //创建哈希表
            HashTable hashTable = new HashTable(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);
                    hashTable.add(emp);
                    break;
                case "list":
                    hashTable.list();
                    break;
                case "find":
                    System.out.println("输入查找编号");
                    id = scanner.nextInt();
                    hashTable.findEmpById(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
                }
            }
        }
    }

    //创建HashTable,管理多条链表
    class HashTable{
        private EmpLinkedList[] empLinkedListArray;
        private int size;//表示有多少条链表
        //构造器
        public HashTable(int size) {
            this.size = size;
            //初始化empLinkedListArray
            empLinkedListArray = new EmpLinkedList[size];
            
            //初始化每条链表
            for(int i = 0;i < size;i++) {
                empLinkedListArray[i] = new EmpLinkedList();
            }
        }
        
        //添加雇员
        public void add(Emp emp) {
            //根据员工id,得到该员工应添加到哪条链表
            int empLinkedListNO = hashFun(emp.id);
            //将emp添加到对应链表
            empLinkedListArray[empLinkedListNO].add(emp);
            
        }
        
        //遍历所有链表,遍历hashTable
        public void list() {
            for(int i = 0;i < size;i++) {
                empLinkedListArray[i].list(i);
            }
        }
        
        //根据输入id,查找雇员
        public void findEmpById(int id) {
            //使用散列函数确定在哪条链表查找
            int empLinkedListNO = hashFun(id);
            Emp emp = empLinkedListArray[empLinkedListNO].findEmpById(id);
            if (emp != null) {
                System.out.printf("在第%d条链表中找到雇员id= %d\n",empLinkedListNO+1,id);
            }else {
                System.out.println("没有该雇员信息");
            }
            
        }
        
        //编写散列函数,使用一个简单取模法
        public int hashFun(int id) {
            return id % size;
        }
    }


    //表示雇员
    class Emp{
        public int id;
        public String name;
        public Emp next;//next默认为null
        public Emp(int id,String name) {
            this.id = id;
            this.name = name;
        }
    }
    //创建EmpLinkedList,表示链表
    class EmpLinkedList{
        private Emp head;//默认为null
        
        //添加雇员到链表
        //1.假定添加雇员时,id是自增的,即id总是从小到大的
        //2.因此将雇员直接加入到链表最后即可
        public void add(Emp emp) {
            //第一个雇员
            if (head == null) {
                head = emp;
                return;
            }
            //如果不是第一个,需要辅助变量
            Emp temp = head;
            while(true) {
                if (temp.next == null) {//说明到链表最后
                    break;
                }
                temp = temp.next;
            }
            //退出时将emp加入链表
            temp.next = emp;
        }
        
        //遍历链表显示雇员信息
        public void list(int no) {
            if (head == null) {//说明链表为空
                System.out.println("第" + (no+1) + "条链表为空");
                return;
            }
            System.out.print("第" + (no+1) + "条链表信息为");
            Emp temp = head;//辅助变量
            while(true) {
                System.out.printf(" => id=%d name=%s\t",temp.id,temp.name);
                if (temp.next == null) {//到最后结点
                    break;
                }
                temp = temp.next;
            }
            System.out.println();//换行
         }
        
        //根据id查找雇员,找到返回Emp,没有找到返回空
        public Emp findEmpById(int id) {
            if (head == null) {
                System.out.println("链表为空");
                return null;
            }
            Emp temp = head;
            while(true) {
                if (temp.id == id) {
                    break;
                }
                //退出
                if (temp.next == null) {
                    temp = null;
                    break;
                }
                temp = temp.next;
            }
            return temp;
        }
    }

     

  • 相关阅读:
    【问题解决】Ubuntu 安装 SeisSol 依赖 easi 报错解决: undefined reference to `H5free_memory‘
    数据结构二叉树顺序存储——堆
    Python turtle 模块可以编写游戏,是真的吗?
    Setter-Based DI in Spring
    网络安全(黑客技术)学习笔记
    浅谈安防视频监控平台EasyCVR视频汇聚平台对于夏季可视化智能溺水安全告警平台的重要性
    应用软件安全编程-20生成强随机数
    Spring的循环依赖问题
    刷完这50个标准库模块:没人比我更懂Python了
    微信小程序实现类似于 vue中ref管理调用子组件函数的方式
  • 原文地址:https://blog.csdn.net/m0_72797089/article/details/127713697