Suppose you are using the priority queue data structure to maintain plane-landing system. As planes arrive near the vicinity of the airport, each plane sends a landing request with the duration (in minutes) that the plane can wait before landing. For example, plane may send the request with value 30. That means that the plane can wait 30 minutes before landing. Once a landing request is received, the request will be added to the plane landing priority queue. Each time when the land method is called, the landing request with lowest wait time will be removed from the priority queue. Develop a C or C++ program that implements this plane landing system with following user interface

Respuesta :

Answer:

#include<iostream>

#include<stdio.h>

using namespace std;

class Node

{

public:

string name;

int waittime;

Node *next;

Node(string n,int w)

{

name=n;

waittime=w;

next=NULL;

}

};

Node *insert(Node *head,string name,int waittime)

{

Node *temp=new Node(name,waittime);

if(head==NULL||head->waittime>waittime)

{

temp->next=head;

head=temp;

}

else

{

Node *p=head;

while(p->next!=NULL)

{

if(p->next->waittime>waittime)

{

temp->next=p->next;

p->next=temp;

break;

}

p=p->next;

}

if(p->next==NULL)

{

p->next=temp;

}

}

return head;

}

Node *remove(Node *head)

{

if(head==NULL)

return NULL;

string name=head->name;

int waittime=head->waittime;

cout<<name<<" with wait time "<<waittime<<" landed"<<endl;

head=head->next;

return head;

}

void print(Node *head)

{

if(head==NULL)

return;

Node *temp=head;

cout<<"All the landing requests: "<<endl;

while(temp!=NULL)

{

cout<<temp->name<<" with waiting time "<<temp->waittime<<endl;

temp=temp->next;

}

}

int main()

{

Node *head=NULL;

cout<<" Welcome to plane landing system\n"<<endl;

int exit=0;

while(1)

{

cout<<"1. Make a landing request"<<endl;

cout<<"2. Land a plane"<<endl;

cout<<"3. List all the landing requests"<<endl;

cout<<"4. Exit\n"<<endl;

cout<<"Select your option: ";

int i;

cin>>i;

string name;

int waittime;

switch(i)

{

case 1:

cout<<"Enter plane name: ";

cin>>name;

cout<<"Enter wait time before landing: ";

cin>>waittime;

head=insert(head,name,waittime);

cout<<"Landing request made"<<endl;

break;

case 2:

head=remove(head);

break;

case 3:

print(head);

break;

case 4:

exit=1;

break;

default:

cout<<"Invalid option...Try again"<<endl;

break;

}

if(exit==1)

break;

}

return 0;

}

Explanation:

Input the code and see the output.

ACCESS MORE