Edit The max number of activities is arbitrary As written in

Edit: The max number of activities is arbitrary. As written in question; program should ask us to input the id of activities unless a negative number is entered. It is needed to select activities from these activities. Max number is up to us.

323as1) Could you please help me to solve this problem? (ONLY USING C++)

Problem:

You are requested to create a class called “Activity”.

You will use ordered link list to hold Activity’s class information.

In order to do that you will use Standard Template Library (STL) to make your codes more generic.

In the main method, you will create an ordered linked list object and add the information, which are entered by the user (id, start time, finish time), to the linked list object.

Id should be greater than or equal 0. Unless the user enters a negative number, the program will continue to ask activity information, else the program will terminate and if the first id number is a negative number, program will give an error message like EMPTY

Sort the linked list according to the finish time.

According to the entered a set of activities, select the activities up to maximum number of activities and print the total duration of selected activities.

Sample runs are below.

Edit: The max number of activities is arbitrary. As written in question; program should ask us to input the id of activities unless a negative number is entered. It is needed to select activities from these activities. Max number is up to us.

Reminder:

-      It is needed to include STL for Linked list.

Enter the id Enter the start time 1 Enter the finish time 2 Enter the id 1 Enter the start time: 3 Enter the finish time 4 Enter the id: 2 Enter the start time 0 Enter the finish time: 6 Enter the id: 3 Enter the start time 5 Enter the finish time Enter the id: 4 Enter the start time 8 Enter the finish time 9 Enter the id 5 Enter the start time 5 Enter the finish time 9 Enter the id -1 Selected activities: 0 1 3 4 Duration of activities 5 Enter the id: 1 Enter the start time 3 Enter the finish time 5 Enter the id: 2 Enter the start time 6 Enter the finish time 8 Enter the id: 3 Enter the start time 1 Enter the finish time: 4 Enter the id: 4 Enter the start time: 4 Enter the finish time Enter the id: 5 Enter the start time Enter the finish time: 10 Enter the id -1 Selected activities 3 4 5 Duration of activities 9

Solution

#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
struct node
{
int id;
   int start;
   int finish;
node *next;
}*p = NULL, *head = NULL, *q = NULL, *newNode = NULL;
int len = 0;
int max = 0;
void create(int id, int start, int finish)
{
newNode = new node;
newNode->id = id;
   newNode->start = start;
   newNode->finish = finish;
newNode->next = NULL;
if (len == 0)
{
head = newNode;
p = head;
p->next = head;
len++;
}
else if (len == 1)
{
p = head;
q = p;
if (newNode->finish < p->finish)
{
newNode->next = p;
head = newNode;
p->next = newNode;
}
else if (newNode->finish > p->finish)
{
p->next = newNode;
newNode->next = head;
}
len++;
}
else
{
p = head;
q = p;
if (newNode->finish < p->finish)
{
newNode->next = p;
head = newNode;
do
{
p = p->next;
}
while (p->next != q);
p->next=head;
}
else if (newNode->finish > p->finish)
{
while (p->next !=head && q->finish < newNode->finish)
{
q = p;
p = p->next;
if (p->next == head && (p->finish < newNode->finish))
{
p->next = newNode;
newNode->next = head;
}
else if (newNode->finish < p->finish)
{
q->next = newNode;
newNode->next = p;
break;
}
}
}
}
   if(max < (finish - start)){ // calculating activities with maximum elapsed time
       max = (finish - start);
   }
}
void traverse(int totalNodes)
{
node *t = head;
int len = 0;
   int totalMax = 0;
   cout<<\"Selected Activities: \";
while (len <= totalNodes) // identifying max activities
{
       if(max == (t->finish - t->start)){
           cout<<t->id;
           totalMax++;
       }
t = t->next;
len++;
}
   cout<<\"\ Duration of activities: \"<<(max*totalMax);
}
int main()
{
   //variables declared
   int len = 0,id,start,finish;
   // displaying menu
   cout<<\"Enter activities and enter -1 to exit\";
   while(true){
       //reading data from user
       cout<<\"Enter id: \";
       cin>>id;
       if(id < 0){ //exit condition
           break;
       }
       else{
           cout<<\"Enter start time: \";
           cin>>start;
           cout<<\"Enter finish time: \";
           cin>>finish;
           create(id,start,finish);
           len++;
       }
   }
   // printing resutls
traverse(len);
}

Edit: The max number of activities is arbitrary. As written in question; program should ask us to input the id of activities unless a negative number is entered
Edit: The max number of activities is arbitrary. As written in question; program should ask us to input the id of activities unless a negative number is entered
Edit: The max number of activities is arbitrary. As written in question; program should ask us to input the id of activities unless a negative number is entered

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site