-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.java
60 lines (49 loc) · 1.39 KB
/
HashTable.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
public class HashTable {
String[] table;
public HashTable(){
table = new String[1000];
}
public int hash(String key) {
int val = 0;
for (int i = 0; i < key.length(); i++) {
val = val + (int)Math.pow(31, i)*key.charAt(i);
}
return val;
}
public void insert(String key) {
int hash = hash(key);
int index = hash % table.length;
while (table[index] != null) {
index = (index + 1) % table.length;
}
table[index] = key;
}
public int get(String key) {
int hash = hash(key);
int index = hash % table.length;
while (table[index] != null) {
if (table[index] == key) {
return index;
}
index = (index + 1) % table.length;
}
return -1;
}
public String Atc_code(String key){
if (get(key) != -1){
String str = String.format("%03d", get(key));
return key+str;
}
return "-1";
}
public void printHashTable() {
System.out.println("\nHash Table: ");
int i;
for (i = 0; i < 1000; i++) {
if (table[i] != null){
System.out.println(table[i] + " " + table[i]);
System.out.println(i);
}
}
}
}