/* * ChainedHashTable.java * * Computer Science 112, Boston University * * Modifications and additions by: * name: * email: */ import java.util.*; // to allow for the use of Arrays.toString() in testing /* * A class that implements a hash table using separate chaining. */ public class ChainedHashTable implements HashTable { /* * Private inner class for a node in a linked list * for a given position of the hash table */ private class Node { private Object key; private LLQueue values; private Node next; private Node(Object key, Object value) { this.key = key; values = new LLQueue(); values.insert(value); next = null; } } private Node[] table; // the hash table itself private int numKeys; // the total number of keys in the table /* hash function */ public int h1(Object key) { int h1 = key.hashCode() % table.length; if (h1 < 0) { h1 += table.length; } return h1; } /*** Add your constructor here ***/ /* * insert - insert the specified (key, value) pair in the hash table. * Returns true if the pair can be added and false if there is overflow. */ public boolean insert(Object key, Object value) { /** Replace the following line with your implementation. **/ return false; } /* * search - search for the specified key and return the * associated collection of values, or null if the key * is not in the table */ public Queue search(Object key) { /** Replace the following line with your implementation. **/ return null; } /* * remove - remove from the table the entry for the specified key * and return the associated collection of values, or null if the key * is not in the table */ public Queue remove(Object key) { /** Replace the following line with your implementation. **/ return null; } /*** Add the other required methods here ***/ /* * toString - returns a string representation of this ChainedHashTable * object. *** You should NOT change this method. *** */ public String toString() { String s = "["; for (int i = 0; i < table.length; i++) { if (table[i] == null) { s += "null"; } else { String keys = "{"; Node trav = table[i]; while (trav != null) { keys += trav.key; if (trav.next != null) { keys += "; "; } trav = trav.next; } keys += "}"; s += keys; } if (i < table.length - 1) { s += ", "; } } s += "]"; return s; } public static void main(String[] args) { /** Add your unit tests here **/ } }