// Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. /** @file HashedDictionary.cpp */ // Separate chaining resolves collisions // PARTIALLY COMPLETE template bool HashedDictionary::add(const KeyType& searchKey, const ItemType& newItem) { // Create entry to add to dictionary HashedEntry* entryToAddPtr = new HashedEntry(newItem, searchKey); // Compute the hashed index into the array int itemHashIndex = getHashIndex(searchKey); // Add the entry to the chain at itemHashIndex if (hashTable[itemHashIndex] == nullptr) { hashTable[itemHashIndex] = entryToAddPtr; } else { entryToAddPtr->setNext(hashTable[itemHashIndex]); hashTable[itemHashIndex] = entryToAddPtr; } // end if return true; } // end add template bool HashedDictionary::remove(const KeyType& searchKey) { bool itemFound = false; // Compute the hashed index into the array int itemHashIndex = getHashIndex(searchKey); if (hashTable[itemHashIndex] != nullptr) { // Special case - first node has target if (searchKey == hashTable[itemHashIndex]->getKey()) { HashedEntry* entryToRemovePtr = hashTable[itemHashIndex]; hashTable[itemHashIndex] = hashTable[itemHashIndex]->getNext(); delete entryToRemovePtr; entryToRemovePtr = nullptr; // For safety itemFound = true; } else // Search the rest of the chain { HashedEntry* prevPtr = hashTable[itemHashIndex]; HashedEntry* curPtr = prevPtr->getNext(); while ((curPtr != nullptr) && !itemFound ) { // Found item in chain so remove that node if (searchKey == curPtr->getKey()) { prevPtr->setNext(curPtr->getNext()); delete curPtr; curPtr = nullptr; // For safety itemFound = true; } else // Look at next entry in chain { prevPtr = curPtr; curPtr = curPtr->getNext(); } // end if } // end while } // end if } // end if return itemFound; } // end remove