Using C++ programing, create a program using singly linked list, that support th
ID: 3679678 • Letter: U
Question
Using C++ programing, create a program using singly linked list, that support the ability to add the following information about an airport: airport code, distance from the last entered airport code to the current one.
Provide a sentinel header which represents LGA, with a distance of zero. Allow the user to provide 3 types of input:
1. Enter new airport information
2. Specify an existing airport code and calculate the distance from LGA to the selected airport using recursion.
3. Specify an existing airport code and calculate the distance from LGA to the selected airport by looping thru the nodes in standard fashion.
4. Also the user should be able to search for the airport they've enter before.
Explanation / Answer
Program:
#include<iostream>
#include<list>
using namespace std;
int find( list<string> airportcode,list<int> airportdis,string cur)
{
int total;
std::list<string>::iterator itcod=airportcode.begin();
std::list<int>::iterator itdis=airportdis.begin();
if(*itcod==cur)
{
return *itdis;
}
else
{
total= *itdis;
airportcode.pop_front();
airportdis.pop_front();
total=total+find( airportcode,airportdis,cur);
return total;
}
}
int main()
{
//store code and distance in list
list<string> airportcode;
list<int> airportdis;
int num,i,dis,total=0, tot=0;
string t,cur;
//read number of airport from user
cout<<"How many airport code you wants to enter: ";
cin>>num;
//read airport details from user
for(i=0;i<num;i++)
{
cout<<"Enter "<<i+1<<" airport code :";
//read airport name
cin>>t;
//insert in linked list
airportcode.push_back(t);
cout<<"Enter "<<i+1<<" airport distance :";
//read distance from last airport
cin>>dis;
airportdis.push_back(dis);
}
cout<<"Enter current airport code: ";
//read current airport code from user
cin>>cur;
//call function to determine disatnce through recursion
tot=find( airportcode,airportdis, cur);
//display total distance calculated by recursion.
cout<<"By recursion total distance from LGA to "<<cur<<" is : "<<tot<<endl;
//declare iterator
std::list<int>::iterator itdis=airportdis.begin();
//use loop to determine distance from airport.
for (std::list<string>::iterator itcod=airportcode.begin(); itcod != airportcode.end(); ++itcod, ++itdis)
{
//check current itearted node is equal to search airport code
if(*itcod==cur)
{
//add distance
total=total+ *itdis;
break;
}
else
{
total=total+ *itdis;
}
}
//distance total distance calculate by looping
cout<<"By looping total distance from LGA to "<<cur<<" is : "<<total<<endl;
return 0;
}
OUTPUT:
How many airport code you wants to enter: 3
Enter 1 airport code :A
Enter 1 airport distance :2
Enter 2 airport code :B
Enter 2 airport distance :3
Enter 3 airport code :C
Enter 3 airport distance :4
Enter current airport code: C
By recursion total distance from LGA to C is : 9
By looping total distance from LGA to C is : 9
--------------------------------
Process exited with return value 0
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.