-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.java
101 lines (86 loc) · 2.35 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
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
// simple Hash Table implemented with separate chaining
// I may implement a Linear Probing version later.
import Queue;
public class HashTable<K, V> {
private int count;
private int numLists;
private Node[] lists;
private static class Node {
private K key;
private V value;
private Node next;
public Node(K kee, V val, Node nex) {
key = kee;
value = val;
next = nex;
}
}
public HashTable() {
return HashTable(997);
}
public HashTable(int siz) {
numLists = siz;
lists = new Node[siz];
}
private int hash(K kee) {
return (key.hashCode() & 0x7fffffff) % numLists;
}
public int size() {
return numLists;
}
public boolean isEmpty() {
return size() == 0;
}
public boolean contains(K kee) {
return get(kee) != null;
}
public V get(K kee) {
int hashed = hash(kee);
for (Node curr = lists[hashed]; curr != null; curr = curr.next) {
if (kee.equals(curr.key)) { return curr.value; }
}
return null;
}
public void insert(K kee, V val) {
if (val == null) {
delete(kee);
return;
}
int hashed = hash(kee);
for (Node curr = lists[hashed]; curr != null; curr = curr.next) {
if (kee.equals(curr.key)) {
curr.value = val;
return;
}
}
count++;
lists[hashed] = new Node(kee, val, lists[hashed]);
}
public void delete(K kee) {
int hashed = hash(kee);
Node curr = lists[hashed];
if (curr == null) { return; }
// first node might be a match
if (kee.equals(curr.key)) {
lists[hashed] = curr.next;
return;
}
//find and delete correct node
for (; curr != null; curr = curr.next) {
if (kee.equals(curr.next.key)) {
curr.next = curr.next.next;
return;
}
}
return; //key not found
}
public Iterable<K> keys() {
Queue<K> q = new Queue<K>();
for (int i = 0; i < numLists; i++) {
for (Node curr = lists[i]; curr != null; curr = curr.next) {
q.enqueue(curr.key);
}
}
return q;
}
}