/
HashTableDemo.java
197 lines (170 loc) · 4.62 KB
/
HashTableDemo.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package io.github.gozhuyinglong.datastructures.hashtable;
/**
* 散列表
*
* @author 码农StayUp
* @date 2021/1/18 0018
*/
public class HashTableDemo {
public static void main(String[] args) {
HashTable hashTable = new HashTable(10);
System.out.println("------------初始化散列表");
hashTable.print();
System.out.println("------------添加关键字 12");
hashTable.add(12);
hashTable.print();
System.out.println("------------添加关键字 13");
hashTable.add(13);
hashTable.print();
System.out.println("------------添加关键字 24");
hashTable.add(24);
hashTable.print();
System.out.println("------------添加关键字 34");
hashTable.add(34);
hashTable.print();
System.out.println("------------添加关键字 44");
hashTable.add(44);
hashTable.print();
}
private static class HashTable {
private final int size; // 散列表大小
private final Node[] table; // 散列表
private HashTable(int size) {
this.size = size;
this.table = new Node[size];
}
/**
* 散列函数 - 除留余数法
*
* @param key
* @return
*/
private int hash(int key) {
return key % size;
}
/**
* 添加关键字
*
* @param key
*/
public void add(int key) {
int hashAddress = hash(key);
if (table[hashAddress] == null) {
table[hashAddress] = new Node(key);
return;
}
add(table[hashAddress], key);
}
/**
* 添加关键字 - 递归
*
* @param node
* @param key
*/
private void add(Node node, int key) {
if (node.getNext() == null) {
node.setNext(new Node(key));
return;
}
add(node.getNext(), key);
}
/**
* 匹配关键字
*
* @param key
* @return
*/
public boolean contains(int key) {
int hashAddress = hash(key);
Node headNode = table[hashAddress];
if (headNode == null) {
return false;
}
return contains(headNode, key);
}
/**
* 匹配关键字 - 递归
*
* @param node
* @param key
* @return
*/
private boolean contains(Node node, int key) {
if (node == null) {
return false;
}
if (node.getKey() == key) {
return true;
}
return contains(node.getNext(), key);
}
/**
* 移除关键字
*
* @param key
*/
public void remove(int key) {
int hashAddress = hash(key);
Node headNode = table[hashAddress];
if (headNode == null) {
return;
}
if (headNode.getKey() == key) {
table[hashAddress] = headNode.getNext();
return;
}
remove(headNode, key);
}
/**
* 移除关键字 - 递归
*
* @param node
* @param key
*/
private void remove(Node node, int key) {
if (node.getNext() == null) {
return;
}
if (node.getNext().getKey() == key) {
node.setNext(node.getNext().getNext());
return;
}
remove(node.getNext(), key);
}
/**
* 打印散列表
*/
public void print() {
for (int i = 0; i < table.length; i++) {
System.out.printf("[%s]\t", i);
if (table[i] != null) {
print(table[i]);
}
System.out.println();
}
}
private void print(Node node) {
if (node == null) {
return;
}
System.out.printf("%s\t", node.key);
print(node.getNext());
}
}
private static class Node {
private final int key; // 关键字
private Node next; // 下一节点
public Node(int key) {
this.key = key;
}
public int getKey() {
return key;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}