void insertList(ListCell* L, HashTable& T); //========================================== // destroyList //========================================== // Delete all the cells in linked list L. //========================================== void destroyList(ListCell* L) { if(L != NULL) { destroyList(L->next); delete L; } } //========================================== // enlarge //========================================== // Double the size of the array in hash table // T. //========================================== void enlarge(HashTable& T) { int oldSize = T.size; int newSize = 2*oldSize; ListCell* oldA = T.A; T = HashTable(newSize); for(int i = 0; i < oldSize; i++) { insertList(oldA[i], T); destroyList(oldA[i]); } } //========================================== // insert //========================================== // Insert key x with associated value v into // hash table T. If x is already a key in // T, then just replace its value by v. //========================================== void insert(string x, string v, HashTable& T) { if(T.load >= 2*T.size) { enlarge(T); } int h = strhash(x.c_str()) % T.size; //----------------------------------------- // Handle the case where x is already // in the table. //----------------------------------------- for(ListCell* p = T.A[h]; p != NULL; p = p->next) { if(p->key == x) { p->value = v; return; } } //----------------------------------------- // Insert a new cell for new key x. //----------------------------------------- T.A[h] = new ListCell(x, v, T.A[h]); } //========================================== // insertList //========================================== // For each (key, value) pair in linked list // L, insert that pair int hash table T. //========================================== void insertList(ListCell* L, HashTable& T) { for(ListCell* p = L; p != NULL; p = p->next) { insert(p->key, p->value, T); } }