public class SeparateChainingHashTable { public SeparateChainingHashTable( ) { this( DEFAULT_TABLE_SIZE ); } public SeparateChainingHashTable( int size ) { theLists = new LinkedList[ nextPrime( size ) ]; for( int i = 0; i < theLists.length; i++ ) theLists[ i ] = new LinkedList( ); } public void insert( Hashable x ) { LinkedList whichList = theLists[ x.hash( theLists.length ) ]; LinkedListItr itr = whichList.find( x ); if( itr.isPastEnd( ) ) whichList.insert( x, whichList.zeroth( ) ); } public void remove( Hashable x ) { theLists[ x.hash( theLists.length ) ].remove( x ); } public Hashable find( Hashable x ) { return (Hashable)theLists[ x.hash( theLists.length ) ].find( x ).retrieve( ); } public void makeEmpty( ) { for( int i = 0; i < theLists.length; i++ ) theLists[ i ].makeEmpty( ); } private static final int DEFAULT_TABLE_SIZE = 101; private LinkedList [ ] theLists; public static void main( String [ ] args ) { SeparateChainingHashTable H = new SeparateChainingHashTable( ); H.insert( new MyInteger( 24 ) ); H.insert( new MyInteger( 48 ) ); H.insert( new MyInteger( 64 ) ); H.remove( new MyInteger( 48 ) ); System.out.println( "done" ); } private static int nextPrime( int n ) { if( n % 2 == 0 ) n++; for( ; !isPrime( n ); n += 2 ) ; return n; } private static boolean isPrime( int n ) { if( n == 2 || n == 3 ) return true; if( n == 1 || n % 2 == 0 ) return false; for( int i = 3; i * i <= n; i += 2 ) if( n % i == 0 ) return false; return true; } }