I hope I am not going against any rules on this site. This is my last follow up question, and I believe I am ready to turn this code in. (It is a homework assignment and I am required to use linked lists) This code is a follow up to this question. I certainly have gained knowledge from this community and am very appreciative of your feedback.
Some things I have implemented since the first iteration:
Implemented enum for a menu instead of using magic numbers, No longer using using namespace std
, No longer using system("pause")
, No longer using std::endl
but now using \n
, Fixed memory leaks
I still neeed to implement an undo/redo stack and fix my edit function because it is not working correctly.
#include "stdafx.h"
#include <iostream>
#include <string>
class List
{
private:
struct node
{
std::string firstName;
std::string lastName;
std::string phoneNumber;
node *next;
};
node *head, *tail;
public:
List()
{
head = NULL;
tail = NULL;
}
void create_contact(std::string first, std::string last, std::string cellNumber) // Create node and add it onto the linked list
{
node *temp = new node;
temp->firstName = first;
temp->lastName = last;
temp->phoneNumber = cellNumber;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
void display_all() const
{
int contactNum = 0;
if (head == NULL) {
std::cout << "You have no contacts.";
}
for (node *curr = head; curr; curr = curr->next)
{
std::cout << "\nContact Number: " << ++contactNum << "\n";
std::cout << "First Name: " << curr->firstName << "\n";
std::cout << "Last Name: " << curr->lastName << "\n";
std::cout << "Phone Number: " << curr->phoneNumber << "\n";
}
}
void display_contact(std::string first, std::string last)
{
bool found;
node *curr = head;
found = false;
int idx = -1;
while (curr != NULL & !found)
{
if (curr->firstName == first && curr->lastName == last)
{
idx++;
found = true;
}
else
{
curr = curr->next;
}
}
if (found)
{
std::cout << "First Name: " << curr->firstName << "\n";
std::cout << "Last Name: " << curr->lastName << "\n";
std::cout << "Phone Number: " << curr->phoneNumber << "\n";
delete_position(idx);
}
else
{
std::cout << "\n" << first << " " << last << " was not found.\n";
}
}
void name_search(std::string titleSearched)
{
bool found;
node *curr = head;
found = false;
while (curr != NULL & !found)
{
if (curr->firstName == titleSearched)
{
found = true;
}
else
{
curr = curr->next;
}
}
if (found)
{
std::cout << "\n" << titleSearched << " was found.\n";
std::cout << "-----------------------------------\n";
std::cout << "First Name: " << curr->firstName << "\n";
std::cout << "Last Name: " << curr->lastName << "\n";
std::cout << "Phone Number: " << curr->phoneNumber << "\n";
}
else
{
std::cout << "\n" << titleSearched << " was not found.\n";
}
}
void delete_position(int pos)
{
if (pos < 1 || head == nullptr)
return;
node *victim{ head };
if (pos == 1) { // deleting head
if (tail == victim) {
head = tail = nullptr;
}
else {
head = head->next;
}
}
else {
// deleting non-head node
node *prev{ head };
for (pos -= 2; pos; --pos) {
if (prev == nullptr)
return;
prev = prev->next;
}
victim = prev->next;
prev->next = victim->next;
if (tail == victim) {
tail = prev;
}
}
delete victim;
}
void pause() {
getchar();
}
};
int main()
{
List Contacts; // create a Contacts item for the List class
int position;
std::string firstName;
std::string lastName;
std::string phoneNumber;
std::string choice;
int x;
enum {
ShowAll = 1,
AddContact,
RemoveContact,
EditContact,
SearchContacts,
ExitContacts
};
bool exit_contacts = false;
while (!exit_contacts) {
std::cout << "\nWhat would you like to do?: " << "\n";
std::cout << "1. Show All Contacts" << "\n";
std::cout << "2. Add A Contact" << "\n";
std::cout << "3. Remove A Contact" << "\n";
std::cout << "4. Edit A Contact" << "\n";
std::cout << "5. Search Contacts" << "\n";
std::cout << "6. Exit The Program" << "\n\n";
std::cin >> choice;
try {
x = std::stoi(choice);
}
catch (...) {
x = 0;
}
switch (x)
{
case ShowAll:
std::cout << "\n";
Contacts.display_all();
std::cout << "\n";
break;
case AddContact:
std::cout << "\nEnter the contacts' first name: ";
std::cin >> firstName;
std::cout << "\nEnter the contacts' last name: ";
std::cin >> lastName;
std::cout << "\nEnter the contacts' phone number: ";
std::cin >> phoneNumber;
Contacts.create_contact(firstName, lastName, phoneNumber);
std::cout << "\n";
break;
case RemoveContact:
std::cout << "\nEnter the contact number of the contact you would like to remove: ";
std::cin >> position;
Contacts.delete_position(position);
break;
case EditContact:
std::cout << "\nTo find the contact you would like to edit: ";
std::cout << "\nEnter the contacts' first name: ";
std::cin >> firstName;
std::cout << "\nEnter the contacts' last name: ";
std::cin >> lastName;
Contacts.display_contact(firstName, lastName);
std::cout << "\nEnter the contacts' NEW first name: ";
std::cin >> firstName;
std::cout << "\nEnter the contacts' NEW last name: ";
std::cin >> lastName;
std::cout << "\nEnter the contacts' NEW phone number: ";
std::cin >> phoneNumber;
Contacts.create_contact(firstName, lastName, phoneNumber);
break;
case SearchContacts:
std::cout << "\nEnter the contacts' first name: ";
std::cin >> firstName;
Contacts.name_search(firstName);
break;
case ExitContacts:
exit_contacts = true;
break;
default:
std::cout << "\n" << choice << " is not an option. Please select a valid option." << "\n";
break;
}
}
Contacts.pause();
return 0;
}
display_contact()
method deletes the object it displays. Are you sure you want to do it that way? \$\endgroup\$