-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.java
51 lines (44 loc) · 1.17 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
class HashTable
{
// hash table size
int hashTableSize = 37;
// array to store items
String [] hashTableArray;
// for counting the number of additional comparisons due to collisions
int collisions;
// include additional functions specified by the user
HashTableFunctions htf;
// constructor
public HashTable ()
{
hashTableArray = new String [hashTableSize];
for ( int i=0; i<hashTableSize; i++ )
hashTableArray[i] = "";
collisions = 0;
htf = new HashTableFunctions (hashTableSize, hashTableArray);
}
// return number of additional collision comparisons
public int getCollisions ()
{ return collisions; }
// stub to point to external function
public int hash ( String s )
{
return htf.hash (s);
}
// inserts string s into the hash table
public void insert ( String s )
{
int h = hash (s);
while (! hashTableArray[h].equals (""))
{
h = (h+1) % hashTableSize;
collisions++;
}
hashTableArray[h] = s;
}
// stub to point to external function
boolean find ( String s )
{
return htf.find (s);
}
}