1: // Created by Frank M. Carrano and Timothy M. Henry. 2: // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. 4: template <class KeyType, class ValueType> 5: bool HashedDictionary<KeyType, ValueType>::remove(const KeyType& searchKey) 6: { 7: bool isSuccessful = false; 8: 9: // Compute the hashed index into the array 10: int hashIndex = getHashIndex(searchKey); 11: if (hashTable[hashIndex] != nullptr) 12: { 13: // Special case - first node has target 14: if (searchKey == hashTable[hashIndex]->getKey()) 15: { 16: hashTable[hashIndex] = hashTable[hashIndex]->getNext(); 17: isSuccessful = true; 18: } 19: else // Search the rest of the chain 20: { 21: auto prevPtr = hashTable[hashIndex]; 22: auto curPtr = prevPtr->getNext(); 23: while ((curPtr != nullptr) && !isSuccessful) 24: { 25: if (searchKey == curPtr->getKey()) 26: { 27: // Found item in chain so remove that node 28: prevPtr->setNext(curPtr->getNext()); 29: isSuccessful = true; 30: } 31: else // Look at next entry in chain 32: { 33: prevPtr = curPtr; 34: curPtr = curPtr->getNext(); 35: } // end if 36: } // end while 37: } // end if 38: } // end if 39: 40: return isSuccessful; 41: } // end remove