Hi, I need help with Recursion in a singly linked list homework. It was a C++ pr
ID: 3703283 • 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 5 times now 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.
Explanation / Answer
SOURCE CODE:
#?include?<iostream.h>
#include<stdio.h>
#include<string.h>
struct node
{
char apcode[30];
int dis;
struct node *link;
};
class aplist
{
private:
struct node *start;
public:
aplist()
{
start=new node;//(struct node *)malloc(sizeof(struct node));;
strcpy(start->apcode,"LGA");
start->dis=0;
start->link=NULL;
}
void addinfo(char str[],int d);
int srchrec(struct node *t,char str[],int addd);
int srchnor(char str[]);
void freemem();
void view();
};
void aplist::view()
{
struct node *t=start;
cout<<" Aiport code with distances from last node: ";
while(t!=NULL)
{
cout<<" "<<t->apcode<<" "<<t->dis;
t=t->link;
}
}
void aplist::addinfo(char str[],int d)
{
struct node *temp;
temp=new node;//(struct airin *)malloc(sizeof(struct airinfo));
strcpy(temp->apcode,str);
temp->dis=d;
temp->link=NULL;
struct node *t=start;
while(t->link!=NULL)
{
t=t->link;
}
t->link=temp;
}
int aplist::srchrec(struct node *t,char str[],int addd)
{
int res;
//struct node *str;
//cout<<str;
if(strcmpi(start->apcode,str)==0)
{
return 0;
}
if(t==NULL)
{
if(start->link!=NULL)
{
res=srchrec(start->link,str,addd+(start->dis));
}
else
return -1;
}
else //
{
// cout<<t->apcode<<" "<<str;
if(strcmpi(t->apcode,str)==0)
{
// cout<<"True";
return addd+(t->dis);
}
else
{
if(t->link!=NULL)
{
res=srchrec(t->link,str,addd+(t->dis));
}
else
{
return -1;
}
}
}
return res;
}
//}//func end
void aplist::freemem()
{
struct node *t=start,*temp;
while(t!=NULL)
{
temp=t->link;
delete temp;
t=temp;
}
}
int aplist::srchnor(char str[])
{
struct node *t=start;
int lastdis=0;
while(t!=NULL)
{
if(strcmpi(t->apcode,str)==0)
{
return lastdis+(t->dis);
}
lastdis=t->dis;
t=t->link;
}
return -1;
}
int main()
{
int choice;
int dist;
char acode[30],ch,more;
aplist l1;
do
{
cout<<"Enter 1 to input airport info";
cout<<" Enter 2 to input airport code to calc dis from LGA using rec";
cout<<" Enter 3 to input airport code to calc dis from LGA normal";
cout<<" Enter 4 to view the entered airport codes";
cout<<" Enter 5 to exit";
cin>>choice;
switch(choice)
{
case 1:
do
{
cout<<" Add Airport Info ";
cout<<" Enter airport code:";
gets(acode);
cout<<" Enter distance from last airport entered:";
cin>>dist;
l1.addinfo(acode,dist);
cout<<" Do you want to add more airport code(y/n):";
cin>>more;
}while(more=='y'||more=='Y');
break;
case 2:
cout<<" Enter airport code whose dis to be measured from LGA(rec):";
gets(acode);
dist=l1.srchrec(NULL,acode,0);
if(dist==-1)
{
cout<<" No such airport is present";
}
else
{
cout<<" Distance of "<<acode<<" from LGA is "<<dist;
}
break;
case 3:
cout<<" Enter airport code whose dis to be measured from LGA(nor):";
gets(acode);
dist=l1.srchnor(acode);
if(dist==-1)
{
cout<<" No such airport is present";
}
else
{
cout<<" Distance of "<<acode<<" from LGA is "<<dist;
}
break;
case 4:
l1.view();
break;
case 5:
l1.freemem();
break;
default:
cout<<" Wrong choice ";
}
cout<<" Do want to go back to main menu(y/n):";
cin>>ch;
}while(ch=='y'||ch=='Y');
l1.freemem();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.