Hi, I need help with this Data Structure and Algarith homework. This is a C++ pr
ID: 3727733 • Letter: H
Question
Hi, I need help with this Data Structure and Algarith homework. This is a C++ programming homework and please use CLASS (ex. PUBLIC, PRIVATE) in the program when you do this.
Again please use CLASS (ex. PRIVATE, PUBLIC)
1. Standard 3 character airport code 2. Distance from New York City's LaGuardia Airport to the specified airport, in miles The head of the list should be represented with an element whose value is 'LGA' and whose distance is 0. Airport codes will be provided in order of closest to furthest away. Create a singly linked list to house each of the airports as a node in the same order in which it was provided (nearest to furthest) Each node should contain both the airport code and the entered distance.Explanation / Answer
// C++ program to create a linked list of Airports
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
// structure to define the node of the list
struct Airport
{
char code[3]; // airport code
float distance;// distance
Airport *next; // pointer to next node
};
// class to define the list of airports
class AirportList{
private:
Airport *head; // head of the list
public:
// constructor to create the head of the list whose code is "LGA" and distance is 0
AirportList()
{
head = new Airport;
strcpy(head->code,"LGA");
head->distance = 0;
head->next = NULL;
}
// function to insert the airports in the order provided
// insertion of nodes at the end of the list
void insert(char *code, float distance)
{
Airport *newAirport = new Airport;
strcpy(newAirport->code,code);
newAirport->distance = distance;
newAirport->next = NULL;
Airport *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newAirport;
}
// function to search for an airport code and print all the codes and distances that follows the airport in the list
void searchAirport(char *code)
{
Airport *temp = head;
while(temp != NULL && strcmp(temp->code,code) != 0)
{
temp=temp->next;
}
if(temp != NULL)
{
temp = temp->next;
while(temp != NULL)
{
cout<<" Code :"<<temp->code<<" Distance : "<<temp->distance;
temp = temp->next;
}
}else{
cout<<" This is the last airport in the list."<<endl;
}
}
};
// end of class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.