-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyHashTable.java
More file actions
63 lines (55 loc) · 1.57 KB
/
MyHashTable.java
File metadata and controls
63 lines (55 loc) · 1.57 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
// stupid lil hashtable 4 myself
import java.util.LinkedList;
import java.util.ArrayList;
public class MyHashTable {
private ArrayList<LinkedList<String>> data;
private boolean[] containsList;
private int numLists;
private int n;
public MyHashTable(int N) {
this.n = 0;
this.numLists = N;
data = new ArrayList<LinkedList<String>>(numLists);
containsList = new boolean[numLists];
}
public void add(String s) {
int hash = hash(s);
System.out.println(s + " " + hash);
n++;
LinkedList<String> list = null;
if (!containsList[hash]) {
list = new LinkedList<String>();
data.set(hash, list);
containsList[hash] = true;
} else {
list = data.get(hash);
}
list.add(s);
}
public boolean exists(String s) {
int hash = hash(s);
LinkedList<String> list = data.get(hash);
return list.contains(s);
}
public int hash(String s) {
int hashScore = 0;
for (int i = 0; i < s.length(); i++) {
hashScore += s.charAt(i)*i;
}
return (hashScore%numLists);
}
public void printData() {
for (LinkedList<String> l : data) {
for (String s : l) {
System.out.print(s + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
MyHashTable ht = new MyHashTable(5);
ht.add("hell4o");
ht.add("hi");
ht.printData();
}
}