The hash table data structure white

1. What is a hash table?

Hash table (also known as hash table), is a direct access to the data structure based on key values. To access records by key is mapped to the position of the table, to speed up the search speed

Popular point is: hash table generally divided into two types:
(1): and an array of a linked list, each linked list corresponding to the array, it can be said by operating the hash table can be operated simultaneously a plurality of lists (simple Ha Greek table)

(2): composed of an array of the binary tree and, by operating the hash table can operate a plurality of binary tree (slightly more complex hash table) while

 

Note: This simple hash table-based

 

2. Learning hash table base preparation (has been previously involved in these things)

* The basic operation of the array

* The basic operation of the list

* How will be combined arrays and linked lists

 

3. For chestnuts

Use hash table to create a simple table of employee information, including employee id, name. Employee information can be made by adding the code, display, look for

 

3-1: Construction of an instance of the class of employees

//相当于构建一个链表
public class Emp {
    public int id;
    public String name;
    public Emp next;//next默认为空
    public Emp(int id, String name){
        super();
        this.id=id;
        this.name=name;
    }
}

 

3-2: Construction of a linked list, complete addition, traversal, query (based on chain operation)

//链表类
public class EmpLinkedList {
    //1.头指针指向第一个Emp,因此这个链表的head是直接指向第一个雇员的
    private Emp head;//默认为null

    //添加员工
    //1.假定添加雇员的时候,id是自增长的,id的分配总是从小到大的
    //2.将该雇员添加到最后
    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;
        // emp.next = null;
    }


    //遍历信息
    public void list(int no) {
        if (head == null) {
            System.out.println("第" + no  + "条链表为空");
            return;
        }
        System.out.print("第" + no  + "条链表的信息为:");
        Emp curEmp = head;
        while (true) {
            System.out.printf("=> id=%d name=%s\t", curEmp.id, curEmp.name);
            if (curEmp.next== null) {
                break;
            }
            curEmp = curEmp.next;
        }
        System.out.println();
    }

    //根据员工的信息进行查找
    //如果查找到了返回Emp,如果没有找到 返回null
    public Emp findEmpById(int id){
        //判断链表是否为空
        if(head==null){
            System.out.println("链表为空");
            return null;
        }
        //辅助指针
        Emp curEmp=head;
        while(true){
            if(curEmp.id==id){
                //说明找到了,此时的curEmp已经为当前员工
                break;
            }
            //退出条件
            if(curEmp.next==null){
                curEmp=null;
                break;
            }
            curEmp=curEmp.next;
        }
        return curEmp;
    }


}

 

3-3: Create a hash table is used to manage multiple lists (in fact, is to create a linked list array)

Note: How to arrays and linked lists?

You can use a hash function, a simple write positioning algorithm list (here uses a simple method to locate the array element number%) of

 //编写一个散列函数,用来定位链表
    public int hashFun(int id) {
        return id % size;
    }
//创建哈希表用来管理多条链表
public 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(Emp emp) {
        //根据员工的id 定位到哪条链表
        int empLinkedListNO = hashFun(emp.id);
        //加入到链表中

        //empLinkedListArray[empLinkedListNO]可能为null
        empLinkedListArray[empLinkedListNO].add(emp);
    }

    //编写一个散列函数,用来定位链表
    public int hashFun(int id) {
        return id % size;
    }

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

    public void findEmpById(int id) {
        //使用散列函数到那一条去找
        int empLinkedNo = hashFun(id);
        Emp emp = empLinkedListArray[empLinkedNo].findEmpById(id);
        if (emp != null) {//找到了
            System.out.println("恭喜你在第" + empLinkedNo + "条链表中找到名为" + emp.name + "的雇员");
        } else {
            System.out.println("没有找到该雇员");
        }
    }


}

 

3-4: wave to the main function

import java.util.Scanner;

//哈希表 可以同时管理多条链表
public class HashTabMain {
    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: 查找雇员");
            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
                    hashTab.add(emp);
                    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;
            }
        }
    }
}

 

3-5: operating results

He published 193 original articles · won praise 70 · views 120 000 +

Guess you like

Origin blog.csdn.net/Lzinner/article/details/103103251