7. temp->prev = nullptr;
fin >> temp->firstName;
fin >> temp->lastName;
fin >> temp->address.streetNumber;
fin >> temp->address.street;
fin >> temp->address.streetType;
fin >> temp->address.city;
fin >> temp->address.state;
fin >> temp->address.zipcode;
fin >> temp->phoneNumber;
fin >> temp->emailAddress;
fin >> temp->twitterHandle;
temp = remove_(temp);
if (currNode == nullptr)
{
head = temp;
currNode = head;
8. }
else
{
currNode->next = temp;
temp->prev = currNode;
currNode = currNode->next;
}
}
fin.close();
return head;
}
//removes underscores from street name and city name
Contact * remove_(Contact * contact)
{
for (int i = 0; i < contact->address.street.size(); i++)
{
if (contact->address.street[i] == '_') contact-
9. >address.street[i] = ' ';
}
for (int i = 0; i < contact->address.city.size(); i++)
{
if (contact->address.city[i] == '_') contact-
>address.city[i] = ' ';
}
return contact;
}
//replace spaces with underscores.
Contact * insert_(Contact * contact)
{
for (int i = 0; i < contact->address.street.size(); i++)
{
if (contact->address.street[i] == ' ') contact-
>address.street[i] = '_';
}
for (int i = 0; i < contact->address.city.size(); i++)
12. Contact* navigateRolodex(Contact * start, Contact * end)
{
//can't start navigating from nowhere.
if (start == nullptr) return nullptr;
//when end is nullptr, we want to go to the end of the list
//so we need to change end to the last item of the list.
if (end == nullptr)
{
end = start;
while (end->next != nullptr)
{
end = end->next;
}
}
Contact* currContact = start;
char command = ' ';
while (command != 'q')
13. {
system("CLS");
displayContact(currContact);
cout << "<<(p)revious, (q)uit, (n)ext>>";
cin >> command;
switch (command)
{
case 'p':
if (currContact != start)
{
currContact = currContact->prev;
}
break;
case 'n':
if (currContact != end)
{
currContact = currContact->next;
}
20. head = contact2;
}
else if (contact2 == head)
{
head = contact1;
}
//take care of other things pointing at swappers, but only if
they are not adjacent. . .
if (contact1->next != nullptr && contact1->next !=
contact2)
contact1->next->prev = contact2;
if (contact2->next != nullptr && contact2->next !=
contact1)
contact2->next->prev = contact1;
if (contact1->prev != nullptr && contact1->prev !=
contact2)
contact1->prev->next = contact2;
if (contact2->prev != nullptr && contact2->prev !=
contact1)
contact2->prev->next = contact1;
21. //take care of swappers previous and next
if (contact1->next == contact2) //adjacent, option 1
{
contact1->next = contact2->next;
contact2->next = contact1;
contact2->prev = contact1->prev;
contact1->prev = contact2;
}
else if (contact2->next == contact1) //adjacent, option 2
{
contact2->next = contact1->next;
contact1->next = contact2;
contact1->prev = contact2->prev;
contact2->prev = contact1;
}
else // not adjacent
{
Contact * temp = contact1->next;
23. {
currContact = sorted;
smallest = sorted;
//find smallest
while (currContact != nullptr)
{
bool smallestContact = false;
switch (sortKey)
{
case FIRST:
if (currContact->firstName < smallest-
>firstName)
smallestContact = true;
break;
case LAST:
if (currContact->lastName < smallest-
>lastName)
smallestContact = true;
break;
24. case STREET:
{
string fullAddress =
to_string(currContact->address.streetNumber) + " " +
currContact->address.street + " " + currContact-
>address.streetType;
string smallestFullAddress =
to_string(smallest->address.streetNumber) + " " + smallest-
>address.street + " " + smallest->address.streetType;
if (fullAddress <
smallestFullAddress) smallestContact = true;
break;
}
case CITY:
if (currContact->address.city < smallest-
>address.city) smallestContact = true;
break;
case STATE:
if (currContact->address.state < smallest-
>address.state) smallestContact = true;
break;
case PHONE:
25. if (currContact->phoneNumber < smallest-
>phoneNumber) smallestContact = true;
break;
case EMAIL:
if (currContact->emailAddress < smallest-
>emailAddress) smallestContact = true;
break;
case TWITTER:
if (currContact->twitterHandle < smallest-
>twitterHandle) smallestContact = true;
break;
}
if (smallestContact)
{
smallest = currContact;
}
currContact = currContact->next;
}
//swap
swap(head, sorted, smallest);
26. sorted = sorted->next;
}
}
void sort(Contact* & head)
{
system("CLS");
cout << "Sort by: " << endl;
displayMenu();
int command;
cin >> command;
if (command != 9) sort(head, (sortBy)command);
}
Contact* search(Contact * head)
{
system("CLS");
//options are same as sortBy enum!!!
27. cout << "Search by: " << endl;
displayMenu();
int command = 0;
cin >> command;
if (command != 9)
{
//sort the list
sort(head, (sortBy)command);
cout << "Search text: ";
string searchString;
cin >> searchString;
//search the list
Contact* start = nullptr, *end = nullptr, *currContact
= head;
bool found;
do
28. {
found = false;
switch (command)
{
case 1:
if (currContact->firstName ==
searchString) found = true;
break;
case 2:
if (currContact->lastName == searchString)
found = true;
break;
case 3:
{
string fullAddress =
to_string(currContact->address.streetNumber) + " " +
currContact->address.street + " " + currContact-
>address.streetType;
if (fullAddress == searchString)
found = true;
break;
29. }
case 4:
if (currContact->address.city ==
searchString) found = true;
break;
case 5:
if (currContact->address.state ==
searchString) found = true;
break;
case 6:
if (currContact->phoneNumber ==
searchString) found = true;
break;
case 7:
if (currContact->emailAddress ==
searchString) found = true;
break;
case 8:
if (currContact->twitterHandle ==
searchString) found = true;
break;
30. }
if (found)
{
if (start == nullptr)
{
start = currContact;
end = currContact; //in case there is
only one. . .
}
else
{
end = currContact;
}
}
currContact = currContact->next;
} while ((end == nullptr || found == true) &&
currContact != nullptr);
if (start == nullptr && end == nullptr)
{