Question

In: Computer Science

This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List...

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++

Solutions

Expert Solution

#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);

    }
  }
}

Related Solutions

C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
This question is in reference to BFS and DFS for data structures and algorithms Consider a...
This question is in reference to BFS and DFS for data structures and algorithms Consider a graph algorithm with a growth function on V and E: f(V, E). How would you convert f(V,E) to f'(V) such that f(V,E)=O(g(n))=f(V)? (That is, convert a growth function of two variables to be of one variable in such a way that the Big-Oh bound for the one variable function will hold for the two variable function.) Explain the steps in creating f', and explain...
How would you show two linked lists are equal? (Java for Data Structures and Algorithms)
How would you show two linked lists are equal? (Java for Data Structures and Algorithms)
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
Write a C++ or Java application to create BOTH Stack & Queue data structures. The application...
Write a C++ or Java application to create BOTH Stack & Queue data structures. The application also creates a "DisplayStackElement" and "DisplayQueueElement" routine. The application must be menu driven (with an option to terminate the application) and provide the following features. Allow insertion of a "Circle" object/structure in the Stack data structures. The Circle contains a "radius" data member. The Circle also uses functions/methods "setRadius", "getRadius" and calculateArea (returns a double data type). Allow insertion of a "Circle" object/structure in...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
By using javaFX as the GUI, design and implement java based algorithms using appropriate data structures...
By using javaFX as the GUI, design and implement java based algorithms using appropriate data structures for the following problem: Use depth-first search to find paths to all the vertices in a graph that are connected to a given start vertex s. A sample input file containing the number of vertices, number of edges and a list of edges called tinyCG.txt is provided for you to test your program.
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
Graph theory unit. 1. List important computing algorithms in Computer Networking based on application of graph...
Graph theory unit. 1. List important computing algorithms in Computer Networking based on application of graph theory. 2. Explain following using an example. a. graph, edges and vertices of the graph, directed graph, in-degree and outdegree of vertex, incident, a path, a distance 3. What is the worst – case memory usage of DFS? 4. Does DFS find the shortest path from start node to some node w ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT