Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions