Question: I got it to work! program accepts airport code and distance into a single linked list and tells you how far it is away from

I got it to work! program accepts airport code and distance into a single linked list and tells you how far it is away from the header, which happens to be LGA airport at distance 0. posting it on here for anybody that needs it. If any experts can clean up the code that would be awesome.

#include

using namespace std; struct Node { string code; double dist; Node* next; Node(string code1, double dist1,Node *next1=NULL) { code = code1; dist = dist1; next = next1; } }; //define class Airportlist { public: Node *head=NULL , *nexta, *data; public: Airportlist() {head = NULL;} void add(string realcode, double realnumber); void display(); void search(); }; //insertion void Airportlist::add(string realcode, double realnumber) {

if (head == NULL) { head = new Node(realcode, realnumber); } else { Node *nextloc = head; while (nextloc->next != NULL) nextloc = nextloc->next; nextloc->next= new Node(realcode, realnumber); }

}

//disp void Airportlist::display() { nexta = head; while (nexta->next) { cout << nexta->code << " " << nexta->dist <<" miles away from LGA " <next; } cout << nexta->code << " " << nexta->dist << " miles away from LGA "<< endl; } //search boi void Airportlist::search() { string aircode; int stoplight = 0; cout << " Enter an airport code :"; cin >> aircode; nexta = head; if (nexta->code == aircode)//compare { stoplight = 1; //if found cout << " Listing airport code and distance that follows it :" << endl; cout << " " << nexta->code << " " << nexta->dist << endl; nexta = nexta->next; } else cout << "notfoud";

}

//main function int main() { int choice; Airportlist item; cout << "enter 1 to add airport, 2 to search, 3 to display list,4 to exit" << endl; cin >> choice; string dummycode; double dummydistance; while (choice!=4) { if (choice == 1) {

cout << "enter code for airport:" << endl; cin >> dummycode; cout << "enter distance:" << endl; cin >> dummydistance; item.add(dummycode, dummydistance); cout << endl;

} if (choice == 2) { item.display(); cout << endl; } if (choice == 3) { item.search(); cout << endl; } cout << "enter 1 to add airport, 2 display list, 3 to search list,4 to exit" << endl; cin >> choice; } return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!