Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Singly Linked List C++ Write a C++ singly linked list program which asks the use

ID: 3680663 • Letter: S

Question

Singly Linked List C++

Write a C++ singly linked list program which asks the user to provide the following information for as many airports as they like:

 
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.

Once the list is built, develop an additional function which allows the user to search for a user-specified code and then output all other codes and distances which follow it in the list.

Explanation / Answer

Answer:

#include<iostream>
#include<string.h>
#include<cstdlib>
using namespace std;
struct linkNode
{
   char airportCode[3];
   int distance;
   linkNode *nextNode;
};
class AirportList
{
   linkNode *t;
public:
   linkNode *airportList;
   AirportList()
   {
       t=new linkNode;
       strcpy(t->airportCode, "LGA");
       t->distance=0;
       t->nextNode=NULL;
       airportList=t;
   }
   void insertAirport()
   {
       char code[3];
       int d;
       cout<<"Enter Airport code:"<<endl;
       cin>>code;
       cout<<" Enter the distance:"<<endl;
       cin>>d;
       t=airportList;
       while(t->nextNode!=NULL)
       {
           t=t->nextNode;
       }
       linkNode *t1=new linkNode;
       strcpy(t->airportCode, code);
       t->distance=d;
       t->nextNode=t1;
       t1->nextNode=NULL;
   }
   void displayList()
   {
       t=airportList;
       cout<<"Airport List:"<<endl;
       while(t->nextNode!=NULL)
       {
           cout<<"Airport code:"<<t->airportCode<<" Distance"<<t->distance<<endl;
           t=t->nextNode;
       }
   }
   void searchAirportList()
   {
       char code[3];      
       cout<<"Enter Airport code:"<<endl;
       cin>>code;
       t=airportList;      
       while(t->nextNode!=NULL)
       {
           if(strcmp(t->airportCode,code)==0)
           {
               cout<<"Distance From LGA is:"<<t->distance<<endl;
               linkNode* t1=t;
               while(t1->NextNode!=NULL)  
               {
                   cout<<"Airport code:"<<t1->airportCode<<" Distance"<<t1->distance<<endl;
                   t1=t1->nextNode;
               }
               break;
           }
           t=t->nextNode;
       }
   }
};
int main()
{
   AirportList airList;
   int ch;
   while(1)
   {
       cout<<"Choices:"<<endl;
       cout<<" 1.Insert Airport"<<endl;
       cout<<" 2.Search Airport"<<endl;
       cout<<" 3.Display all Airports"<<endl;
       cout<<" Your choice:"<<endl;
       cin>>ch;
       if(ch==1)
       {
           airList.insertAirport();
       }
       else if(ch==2)
       {
           airList.searchAirportList();
       }
       else if(ch==3)
       {
           airList.displayList();
       }
       else
           break;
   }
   return 0;
}
  
      

Output:

Choices:

1.Insert Airport

2.Search Airport

3.Display all Airports

Your choice:

1

Enter Airport code:

abc

Enter the distance:

10

Choices:

1.Insert Airport

2.Search Airport

3.Display all Airports

Your choice:

1

Enter Airport code:

bcd

Enter the distance:

20

Choices:

1.Insert Airport

2.Search Airport

3.Display all Airports

Your choice:

3

Airport List:

Airport code:abcDist10

Airport code:bcdDist20

Choices:

1.Insert Airport

2.Search Airport

3.Display all Airports

Your choice: