public class Lab6 {
final static int TABLE_SIZE = 7;
public static void main
(String[] args
) {
HashTable T = new HashTable(TABLE_SIZE);
T.initializeTable();
T.insert('J');
T.insert('N');
T.insert('S');
T.insert('X');
T.insert('B');
T.insert('W');
T.printTable();
System.
out.
println("Table printed above.");
T.searchAndPrint('W');
T.searchAndPrint('N');
T.searchAndPrint('A');
} // end main
} // end Lab6
/* ------------------------------------------------------------------------------------------ */
class HashTableNode {
char key;
HashTableNode link;
} //end class
/* ------------------------------------------------------------------------------------------ */
class HashTable {
HashTableNode[] entryArray;
int tableSize;
// a single argument constructor is included
HashTable(int sizeOfTable) {
tableSize = sizeOfTable;
entryArray = new HashTableNode[sizeOfTable];
}
/* the HashTable methods follow */
/* ------------------------------------------------------------------------------------- */
// h1(K) is the hash function that maps a character key K into the index of a table entry
int h1(char K) { // the hash function
return (1+ (int)(K) - (int)('A')) % tableSize;
} // end h1
/* ------------------------------------------------------------------------------------- */
void initializeTable() {
int i;
for ( i= 0; i < tableSize; i++) entryArray[i] = null;
} //end initializeTable
/* --------------------------------------------------------------------------------------- */
/* refine the following Program Strategy into executable Java code */
HashTableNode insert1(char K, HashTableNode L) {
HashTableNode N;
(Insert a new HashTableNode with key K in the sorted list L)
} //end insert1
/* ------------------------------------------------------------------------------------- */
void insert(char K) {
// Call a recursive auxiliary insert1 function to insert key K on the
// list pointed to by entryArray[h1(K)] in alphabetical order of the keys
// update entryArray[h1(K)] to point to new list
entryArray[h1(K)] = insert1(K,entryArray[h1(K)]);
} //end insert
/* ------------------------------------------------------------------------------------- */
HashTableNode find1(char K, HashTableNode L) {
(Walk through the sorted list L)
( Return null if K is not on the list)
( Otherwise return the HashTableNode that contains K)
} //end find1
/* ------------------------------------------------------------------------------------- */
HashTableNode find(char K) {
// use the recursive auxiliary function find1(K,L) to search for and
// return a pointer to the node of list L containing key K, or return
// null if there was no node on list L containing a key K
return find1(K, entryArray[h1(K)]); // returns null if K was not on list
} //end find
/* ------------------------------------------------------------------------------------- */
void printTable() {
int i; HashTableNode L;
for (i = 0; i < tableSize; i++) {
L = entryArray[i];
System.
out.
print("T[" + i
+ "] = (");
while (L != null) {
System.
out.
print("-"+ (1 + (int)(L.
key) - (int) ('A')));
if (L.
link != null) System.
out.
print(',');
L= L.link;
}
}
} //end printTable
/* ------------------------------------------------------------------------------------- */
void searchAndPrint(char K) {
HashTableNode N;
System.
out.
println("Searching for key '" + K
+ "' in Table T");
N = find(K);
if (N != null) {
System.
out.
println(" Found " + N.
key);
} else {
System.
out.
println(" key '" + K
+ "' not Found in Table T");
}
} //end searchAndPrint
/* ------------------------------------------------------------------------------------- */
} //end class Lab6