// remove the object from hash table void hashing::removeObject(person &personObj) { int index = Hash(personObj.getName()); item* delPtr; item* p1; item* p2; //case 0: bucket is empty if(HashTable[index] == NULL) cout << "Person not found in the hash table!\n"; //case 1: only 1 item contained in bucket and that item has matching name else if( HashTable[index]->hashedPerson == &personObj && HashTable[index]->next == 0) { HashTable[index] = NULL; cout << personObj.getName() << " item not found!\n"; } //case 2: match is located in the first item in the bucket but there are more items in the bucket else if(HashTable[index]->hashedPerson == &personObj) { delPtr = HashTable[index]; HashTable[index] = HashTable[index]->next; delete delPtr; delPtr = 0; cout << personObj.getName() << " is removed from the hash table!\n"; numberofentries--; } //case 3: bucket contains items but first item is not a match else { p1 = HashTable[index]->next; p2 = HashTable[index]; while(p1 != 0 && p1->hashedPerson != &personObj) { p2 = p1; p1 = p1->next; } //case 3.1 - no match if(p1 == 0) { cout << "Person not found in the hash table!\n"; } //case 3.2 - match is found else { delPtr = p1; p1 = p1->next; p2->next = p1; delete delPtr; delPtr = 0; cout << personObj.getName() << " is removed from the hash table!\n"; numberofentries--; } } }
void person::sendMessage(person &friendName, string message) { bool found = false; if (&friendName == this) { cout << "You cannot send a message to yourself." << endl; return; } if (friends.size() == 0) { cout << "In order to send a message, you need to be friends with this person"; return; } for (int i = 0; i < friends.size(); i++ ) { if (friends[i]->name == friendName.name) { found = true; } } if(found) { string messageName = getDate() + "\n" + this->name + " " + message + "\n"; string firstName = friendName.getName(); text[firstName].push_back(messageName); friendName.receiveMessage(friendName, messageName); } else { cout << "In order to send a message, you need to be friends with this person"; } };
/**************************** addObject **********************************/ void hashing::addObject(person &personObj) { int tempCollision = 0; int index = Hash(personObj.getName()); cout << personObj.getName() << ": [" << index << "]\n" ; if(HashTable[index] == NULL) { HashTable[index] = new item; HashTable[index]->hashedPerson = &personObj; HashTable[index]->next =0; cout << personObj.getName() << " is added!\n"; } else { item* Ptr = new item; Ptr->hashedPerson = HashTable[index]->hashedPerson; Ptr->next = HashTable[index]->next; HashTable[index]->hashedPerson = &personObj; HashTable[index]->next = Ptr; cout << personObj.getName() << " is added!" << endl; numberofcollisions = numberofcollisions +1; cout << numberofcollisions << endl; } numberofentries++; //cout << "People in this index[" << Hash(personObj.getName()) << "] :" << hashing::NumberOfItemsInIndex(Hash(personObj.getName())) << endl; if(getLoadingfactor() > 0.75) { reHash(); } }