-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashTable.java
More file actions
32 lines (32 loc) · 1021 Bytes
/
MyHashTable.java
File metadata and controls
32 lines (32 loc) · 1021 Bytes
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
class MyHashTable {
MyLinkedList<WordEntry> [] hash = new MyLinkedList[26];
public MyHashTable() {
for (int i=0; i<26; i++) {
hash[i] = new MyLinkedList<WordEntry>();
}
}
public int getHashIndex(String str) { //works for A-Z, a-z, 0-9
str = str.toLowerCase();
int ans = str.charAt(0)-97;
if(ans>=0 && ans<26) return ans;
else return str.charAt(0)-48;
}
public void addPositionsForWord(WordEntry w) {
MyLinkedList<WordEntry> myhash = hash[getHashIndex(w.word)];
Node<WordEntry> temp = myhash.head;
while(temp!=null) {
if(w.word.equals(temp.data.word)) break;
temp = temp.next;
}
if (temp==null) {
myhash.addFirst(w);
}
else {
Node<Position> temp2 = w.word_position.head;
while(temp2!=null) {
temp.data.word_position.addFirst(temp2.data);
temp2 = temp2.next;
}
}
}
}