-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHashApp.java
More file actions
132 lines (114 loc) · 2.1 KB
/
HashApp.java
File metadata and controls
132 lines (114 loc) · 2.1 KB
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 HashTest;
/**
* @time Mar 23, 2018
* @author akun
* @description
*/
class DataItem
{
private int value;
public DataItem(int value)
{
this.value = value;
}
/**
* @return the value
*/
public int getValue()
{
return value;
}
/**
* @param value the value to set
*/
public void setValue(int value)
{
this.value = value;
}
}
public class HashApp
{
public DataItem[] arr;// 哈希表的底层是由数组实现的
public int size;// 当前数组的最大长度
public DataItem newItem;// 新插入的节点
public int currentSize; // 当前数组有几个元素
// 初始化
public HashApp(int maxSize)
{
arr = new DataItem[maxSize];
size = maxSize;
currentSize = 0;
}
// 哈希化
public int hash(int value)
{
return value % size;
}
// 添加数据
public void insert(int value)
{
// 如果满了,则返回
if (isFull())
{
System.out.println("is full...");
return;
}
// 计算哈希化后的索引
int index = hash(value);
newItem = new DataItem(value);
// 当前小标的数组元素不为空,说明有元素了,index递增
while (arr[index] != null)
{
index++;
// 防止index超过数组最大索引下标
index = hash(index);
}
arr[index] = newItem;
++currentSize;
}
// 删除数据
public void delete(int value)
{
int index = hash(value);
while (arr[index] != null)
{
if (arr[index].getValue() == value)
{
arr[index] = null;
return;
}
index++;
index = hash(index);
}
}
// 查找数据
public DataItem find(int value)
{
int index = hash(value);
// 疑问? 万一为空的这个点在之前被删除了呢
while (arr[index] != null)
{
if (arr[index].getValue() == value)
{
return arr[index];
}
index++;
index = hash(index);
}
return null;
}
// 哈希表无法有序的遍历!所以这里遍历只是查看一个数据的分布情况
public void display()
{
for (DataItem arr : arr)
{
if (arr != null)
System.out.print(arr.getValue() + " ");
}
System.out.println();
}
public boolean isFull()
{
return currentSize == size;
}
}