#include #include using namespace std; class StringNode { // establish class StringNode for Linked List private: StringNode *next; // points to next node in the list string elem; // string variable for airport code double miles; // double to hold distance from LGA in miles public: friend class StringLinkedList; // gives StringLinkedList access to StringNode private members }; class StringLinkedList { private: StringNode *head; // points to head of the list StringNode *priorloc; // points to prior node location on list StringNode *nextloc; // points to next node location on list public: StringLinkedList(); // empty list constructor ~StringLinkedList(); // destructor bool empty() const; // function to test for empty list void addNode(const string& e, const double& m); // add nodes to end void removeFront(); // remove nodes as part of destructor void display(); // display list void search(const string&); // finds searched value in list }; StringLinkedList::StringLinkedList() // constructor parameters { head = NULL; // initialize head to NULL priorloc = NULL; // initialize priorloc to NULL nextloc = NULL; // initialize nextloc to NULL } StringLinkedList::~StringLinkedList() // destructor { while (!empty()) removeFront();} bool StringLinkedList::empty() const // checks for empty list { return head == NULL;} int main () { StringLinkedList list = StringLinkedList(); // define list as an object of StringLinkedList string value; // hold airport code double dist; // hold distance in miles to LGA char again; // do-while loop option list.addNode("LGA", 0); do // input data { cout << "Enter the 3 letter airport code: " << endl; cin >> value; cout << "Enter the miles to the airport from La Guardia Airport: " << endl; cin >> dist; list.addNode(value, dist); cout << endl; cout << "Do you want to add another airport location and distance? (Y/N): "; cin >> again; cout << endl; } while (again == 'Y' ||again == 'y'); // user option to adds as many as wanted int choice; // variable for second do-while loop do // user input menu option { cout << "The following are your choices.\n"; cout << "Please enter the number for the selection you want: \n"; cout << endl; cout << "1. Print listing of all airport codes and miles from LGA\n"; cout << "2. Search for airport and print\n"; cout << "3. Quit Program\n"; cin >> choice; while (choice < 1 || choice > 3) // input validation { cout << "The valid choices are 1 through 3. Please\n" << "select one of those." << endl; cin >> choice; } switch (choice) { case 1: // display function described below list.display(); cout << endl; break; case 2: // search function described below {string searchValue; cout << "Which airport are you looking for: "; cin >> searchValue; list.search(searchValue); cout << endl; break;} case 3: // ends program cout << "Thank you for using this program.\n"; cout << "Have a nice Day.\n"; cout << endl; } }while (choice != 3); return 0; } /* Function creates and adds node to end of list. Populates with two variables in data portion of node. Called in the the first do-while loop. */ void StringLinkedList::addNode(const string& e, const double& m) { StringNode* v = new StringNode; v-> elem = e; v-> miles = m; v-> next = NULL; if ( priorloc == NULL ) { head = v; } else { priorloc->next = v; } priorloc = v; } /* Function displays linked list. */ void StringLinkedList::display() { StringNode* temp = head; while (temp != NULL) { cout << temp-> elem << " " << temp-> miles << " miles" << endl; temp = temp-> next; } delete(temp); } /* Function searches for inputed value. Once value is found, it creates a new linked list. It creates and populates another nodes with values that follow the found value on the initial list. It displays the new list as a separate command to the display function. If not found, it so informs the user. */ void StringLinkedList::search(const string& e) { StringNode *temp = head; // create temp node bool found = false; while (temp != NULL) // loops until end of list { if (temp-> elem == e) // if search value is found { found = true; StringLinkedList newList = StringLinkedList(); // point to this node newList.addNode(temp-> elem, temp-> miles); // create a new list, StringNode *current = temp-> next; // point to this list while (current != NULL) // until end of this list { newList.addNode(current->elem,current->miles); // add node to new list and populates with existing data current = current-> next; } newList.display(); // display new list } temp = temp-> next; // otherwise keep searching } if (!found){ cout << "There is no airport code that matches what you entered." << endl; cout << "Please select again." << endl; cout << endl; } } // remove function to support destructor command void StringLinkedList::removeFront() { StringNode* old = head; head = old-> next; delete old; }