Java Reference
In-Depth Information
if (numWords < MaxWords) { //if table is not full
first = addToTable(wordTable, word, -loc, first);
++numWords;
}
else out.printf("'%s' not added to table\n", word);
}
printResults(wordTable, first);
in.close();
out.close();
} // end main
public static int search(WordInfo[] table, String key) {
//search for key in table; if found, return its location; if not,
//return -loc if it must be inserted in location loc
int wordNum = convertToNumber(key);
int loc = wordNum % N + 1;
int k = wordNum % (N - 2) + 1;
while (!table[loc].word.equals(Empty) && !table[loc].word.equals(key)) {
loc = loc + k;
if (loc > N) loc = loc - N;
}
if (table[loc].word.equals(Empty)) return -loc;
return loc;
} // end search
public static int convertToNumber(String key) {
int wordNum = 0;
int w = 3;
for (int h = 0; h < key.length(); h++) {
wordNum += key.charAt(h) * w;
w = w + 2;
}
return wordNum;
} //end convertToNumber
public static int addToTable(WordInfo[] table, String key, int loc, int head) {
//stores key in table[loc] and links it in alphabetical order
table[loc].word = key;
table[loc].freq = 1;
int curr = head;
int prev = -1;
while (curr != -1 && key.compareTo(table[curr].word) > 0) {
prev = curr;
curr = table[curr].next;
}
table[loc].next = curr;
if (prev == -1) return loc; //new first item
table[prev].next = loc;
return head; //first item did not change
} //end addToTable
Search WWH ::




Custom Search