In: Computer Science
This is a C++ based question that involves Data Structures and Algorithms.
Q.
Application: Linked List of Bus Transit and Passengers
You are to implement a C++ program for City Bus Transit using linked list data structure to maintain record of passengers. Specifically, you are to implement the following methods/functions:
For Passenger:
o A function which can create a new node of the linked list using new for each newpassenger
o A function that prints the time of single passenger in the bus
o A function that prints time in the bus of every passenger in the linked list
o A function that increments time of each passenger in the bus by 1 minute
o A function that returns the total number of passengers in the linked list
o A function that returns the average time a passenger spends in the bus
o A function that can remove kth passenger from the linked list
o A function that removes all the passengers of the linked list
Please write a detailed code with comments because I am very new to C++
#include <iostream>
#include <string>
using namespace std;
//
/**Node class to represent a passeger where name is name of
* passenger and time is time spend by passenger in bus**/
class Node {
public:
string name;
int time;
Node* next;
//constructor
Node(string n,int t){
name=n;
time=t;
next=NULL;
}
};
//function to create passenger
Node* newpassenger(string name,Node* head){
Node* passenger=new Node(name,0);
//if first passenger create
if(head==NULL){
head=passenger;
}else{
Node* temp=head;
while(temp->next){
temp=temp->next;
}
temp->next=passenger;
}
return head;
}
//function to print specefic passenger time
void single_passenger_time(int k,Node* head){
Node* temp=head;
if(temp==NULL){
cout<<"bus is empty\n";
return;
}
for(int i=0;i<=k;i++){
if(i==k){
cout<<"Time of "<<k<<"passenger is:-"<<temp->time<<endl;
}
temp=temp->next;
if(temp==NULL){
cout<<"given index is out of bound(not present in bus\n";
}
}
}
//print time of all passenger
void all_passenger_time(Node* head){
Node* temp=head;
int k=0;
while(temp){
cout<<"Time of "<<temp->name<<" is:-"<<temp->time<<endl;
temp=temp->next;
k++;
}
}
//increase time by 1
void increment_time_by_1(Node* head){
Node* temp=head;
while(temp){
temp->time=1+temp->time;
temp=temp->next;
}
}
//return total number of passenger
int total_passenger_in_bus(Node* head){
Node* temp=head;
int k=0;
while(temp){
temp=temp->next;
k++;
}
return k;
}
//return average time spend by passenger
double average_time_of_passenger_in_bus(Node* head){
Node* temp=head;
int k=0;
double t=0;
while(temp){
t+=temp->time;
temp=temp->next;
k++;
}
return k==0?0:((t*1.0)/k);
}
//remove kth node starting from 0,like in array start from 0;
Node* remove_kth_passenger(int k,Node* head){
if(head==NULL)return NULL;
if(k==0){
cout<<head->name<<" will remove\n";
Node *new_head=head->next;
delete(head);
return new_head;
}
k--;
Node* temp=head;
while(temp&&k){
temp=temp->next;
k--;
}
if(temp&&temp->next){
Node* remove_node=temp->next;
cout<<remove_node->name<<" will remove\n";
temp->next=remove_node->next;
//delete will free the memory
delete(remove_node);
}
return head;
}
//delete all passenger
Node* remove_all_passenger(Node* head){
Node *prev=head;
while(head){
head=head->next;
delete(prev);
prev=head;
}
if(prev)delete(prev);
return NULL;
}
//stating of programe
int main(){
Node* head=NULL;
cout<<"choose the option to perform following operation\n";
while(true){
cout<<"\n\n1.create new passenger\n2.print time of specific passenger\n3.print time of all passenger \n4.increment time of each passenger by 1\n5.return total number of passenger\n6.returns the average time a passenger spends in the bus\n7.remove kth passenger from the linked list\n8.removes all the passengers of the linked list\n9.End the programme\n\n\n";
int c;
string name;
cin>>c;
//swith case to perform descired option
switch(c){
case 1:
cout<<"enter name of passenger\n";
cin>>name;
head=newpassenger(name,head);
break;
case 2:
cout<<"enter inde of passenger to print time\n";
int n;
cin>>n;
single_passenger_time(n,head);
break;
case 3:
cout<<"time of all passenger are:-\n";
all_passenger_time(head);
break;
case 4:
increment_time_by_1(head);
break;
case 5:
cout<<"Total number of passenger are\n"<<total_passenger_in_bus(head)<<endl;
break;
case 6:
cout<<"the average time a passenger spends in the bus\n"<<average_time_of_passenger_in_bus(head)<<endl;
break;
case 7:
cout<<"enter the index of passenger to be remove(starting from 0)\n";
int i;
cin>>i;
head=remove_kth_passenger(i,head);
break;
case 8:
cout<<"all passenger are remove\n";
head=remove_all_passenger(head);
break;
case 9:
exit(0);
break;
default:
cout<<"Enter proper case(1-9)\n";
exit(0);
}
}
}