Java Reference
In-Depth Information
1 class MyHashSet<AnyType>
2 {
3 public MyHashSet( )
4 { this( 101 ); }
5
6 public MyHashSet( int numLists )
7 { lists = new Node[ numLists ]; }
8
9 public boolean contains( AnyType x )
10 {
11 for( Node<AnyType> p = lists[ myHashCode( x ) ]; p != null; p = p.next )
12 if( p.data.equals( x ) )
13 return true;
14
15 return false;
16 }
17
18 public boolean add( AnyType x )
19 {
20 int whichList = myHashCode( x );
21
22 for( Node<AnyType> p = lists[ whichList ]; p != null; p = p.next )
23 if( p.data.equals( x ) )
24 return false;
25
26 lists[ whichList ] = new Node<AnyType>( x, lists[ whichList ] );
27 return true;
28 }
29
30 private int myHashCode( AnyType x )
31 { return Math.abs( x.hashCode( ) % lists.length ); }
32
33 private Node<AnyType> [ ] lists;
34
35 private static class Node<AnyType>
36 {
37 Node( AnyType d, Node<AnyType> n )
38 {
39 data = d;
40 next = n;
41 }
42
43 AnyType data;
44 Node<AnyType> next;
45 }
46 }
figure 20.20
Simplified implementation of separate chaining hash table
Search WWH ::




Custom Search