Hi, I need help with Recursion in a singly linked list homework. It was a C++ pr
ID: 3703550 • Letter: H
Question
Hi, I need help with Recursion in a singly linked list homework. It was a C++ programming homework question will be posted below. Please use CLASS to do this program and ..... Also, include the output.....One more thing if you don't know how this program work then doesn't answer I submitted this question almost 9 times now on to the Chegg. But they don't answer them correctly or the program not running or the options doesn't come out correctly. Please read the question.
--------------------- Question -------------------
Recursion in a Singly Linked List
----------------------------------------------------
Using a singly linked list, 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.
----------------------------------
Don't forget to use CLASS
------------------------------------
The output should have these things
cout << "1. Add an Airport ";
cout << "2. Find an Airport - Recursively ";
cout << "3. Find an Airport - Standard Looping ";
cout << "4. Show all Airports ";
cout << "5. Exit Program ";
cout << "Enter your choice: ";
Explanation / Answer
//start of program
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
//node declaration with name airport
struct airport
{
//declaring variables for variour parameters of airport
int airport_code;
int distance_from_last;
struct airport *next_airport;
}*first_airport;
//singly linked list of airports class declaration
class airport_list
{
public:
airport* create_airport(int, int);
void add_airport(int, int);
void find_airport_recur(int);
void find_airport_stanloop(int);
void display_all();
airport_list()
{
first_airport = null;
}
}
airport *airport_list::create_airport(int code, int distance){
struct airport *temp;
temp = new(struct airport);
if(temp == null) return 0;
else{
temp->airport_code = code;
temp->distance_from_last = distance;
temp->next_airport = null;
return temp;
}
}
void airport_list::add_airport(int code, int distance){
struct airport *temp, *p;
temp = create_airport(code, distance);
p = first_airport;
while(p->next_airport != null){
p = p->next_airport;
}
temp->next_airport = null;
p->next_airport = temp;
}
void airport_list::find_airport_stanloop(int code){
struct airport *temp;
temp = first_airport;
int distance;
while(temp->airport_code != code){
distance = distance + temp->distance_from_last;
temp = temp->next_airport;
}
cout<< "distance from first node is "<<distance;
}
void airport_list::display_all(){
struct airport *temp;
temp = first_airport;
cout<< "airports list";
while(temp->airport_code != null){
cout<< temp->airport_code<<" -> ";
temp = temp->next_airport;
}
}
main(){
int choice;
airport_list al;
first_airport = null;
while(1){
cout << "1. Add an Airport ";
cout << "2. Find an Airport - Recursively ";
cout << "3. Find an Airport - Standard Looping ";
cout << "4. Show all Airports ";
cout << "5. Exit Program ";
cout << "Enter your choice: ";
cin >> choice;
switch(choice){
case 1:
int code, distance;
cout<< "enter airport code";
cin>> code;
cout<< "enter distance from previous airport;"
cin>> distance;
if(first_airport == null) al.create_airport(code, distance);
else al.add_airport(code, distance);
break;
case 2:
int code;
cout<< "enter airport code";
cin>> code;
al.find_airport_recur(code);
break;
case 3:
int code;
cout<< "enter airport code";
cin>> code;
al.find_airport_stanloop(code);
break;
case 4:
al.display_all();
break;
case 5:
return;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.