文章

哈希表

哈希表

哈希表

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

常见的哈希表结构 数组+链表 数组+二叉树

哈希表可以同时管理多条链表

哈希表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.learn.hash;
import java.util.Scanner;

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

        //创建哈希表
        HashTable hashTab = 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);
                    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;
            }
        }

    }
}


class HashTable{
    EmpLinkedList[] empLinkedListArray;
    private int size;
    public HashTable(int size)
    {
        empLinkedListArray =new EmpLinkedList[size];
        for(int i=0;i<size;i++)
        {
            EmpLinkedList list =new EmpLinkedList();
            empLinkedListArray[i]=list;
        }
        this.size=size;
    }

    public void Add(Emp value)
    {
        EmpLinkedList list = empLinkedListArray[HashFun(value.id)];
        list.Add(value);
    }

    //散列函数
    public int  HashFun(int id)
    {
        return id%size;
    }

    public void List()
    {
        for(int i=0;i<empLinkedListArray.length;i++)
        {
            empLinkedListArray[i].List();
        }
    }

}


class Emp{
    public int id;
    public String name;
    public Emp(int id,String name)
    {
        this.id=id;
        this.name=name;
    }
    public void ToString()
    {
        System.out.println("id:"+id+" name:"+name);
    }
    public Emp next;

}

class EmpLinkedList
{
    public Emp head;

    public EmpLinkedList()
    {
        head=new Emp(0,null);
    }

    public void Add(Emp value)
    {
        Emp  temp = head;
        while(temp.next!=null)
        {
            temp = temp.next;
        }
        temp.next=value;
    }

    //list 所有元素
    public void List()
    {
        Emp temp=head.next;
        while (temp!=null)
        {
            temp.ToString();
            temp=temp.next;
        }
    }
}
本文由作者按照 CC BY 4.0 进行授权