Presentation is loading. Please wait.

Presentation is loading. Please wait.

IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.

Similar presentations


Presentation on theme: "IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering."— Presentation transcript:

1 IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Bus Queue (Program) 1Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

2 IIT Bombay Data Structures Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay2 Use queue to represent arrival of passengers Each passenger is given a ‘unique id’ is assigned arrival time (arbitrarily, for now) Structure: personInfo Contains information of people (ID and arrival time) that arrive and stand in the queue at the bus stop Queue: busQueue Our Queue is of type structure ‘personInfo’ So, contains information of all people, as above

3 IIT Bombay Functions Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay3 Functions to handle arrival of a person, and arrival of a bus Function 1: personArrives Pushes the information of the person on the queue ‘busQueue’ (includes person-id and arrival time) Function 2: boardBus Bus arrives Depending on the available seats in the bus, people in the bus queue board the bus. Wait time of each person boarding the bus, is calculated Note: if the bus arrives earlier and has not departed, when a person arrives, the waiting time is 0 for the person.

4 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay4 class busStop { private: static int id; queue busQueue; public: void personArrives(int time); void boardBus(int busArrivalTime, int availableSeats); }; //End of class int busStop::id = 1000; Queue 1: busQueue of type structure ‘personInfo’. Each record in a queue will contain the members of structure ‘personInfo’. struct personInfo{ int personID; int arrivalTime; }; //End of structure

5 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay5 void busStop::personArrives(int time) { personInfo person; person.personID = ++id; person.arrivalTime = time; busQueue.push(person); cout << "Person: " << busQueue.back().personID << ", " << busQueue.back().arrivalTime << endl; } //End of function ID and arrival time of the person is pushed in the queue ‘busQueue’. Creating an object of structure ‘personInfo’

6 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay6 void busStop::boardBus(int busArrivalTime, int availableSeats) { int i, count = 0, waitingTime, personArrivalTime = 0; // insert code for boarding the bus (Given on next slide) cout << "\t" << count << " people boarded the bus" << endl; if (!busQueue.empty()) cout << "Next person in the queue: " << busQueue.front().personID << endl; else cout << "No body waiting in the bus queue" << endl; }//End of function

7 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay7 // code for boarding the bus for(i=1;i<=availableSeats;i++) { if (!busQueue.empty()) { personArrivalTime = busQueue.front().arrivalTime; if (busArrivalTime < personArrivalTime) waitingTime = 0; else waitingTime = busArrivalTime - personArrivalTime; cout << "\tWaiting time of " << busQueue.front().personID; cout << ": " << waitingTime << endl; count++; busQueue.pop(); }

8 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay8 int main() { busStop Bstop; int busArrivalTime; Bstop.personArrives(1415); Bstop.personArrives(1419); Bstop.personArrives(1421); Bstop.personArrives(1422); Bstop.personArrives(1427); busArrivalTime = 1430; cout << "Bus arrived at " << busArrivalTime << endl; Bstop.boardBus(busArrivalTime,2); busArrivalTime = 1440; cout << "Bus arrived at " << busArrivalTime << endl; Bstop.boardBus(busArrivalTime,7); return 0; } //End of main

9 IIT Bombay Output Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay9 Person: 1001, 1415 Person: 1002, 1419 Person: 1003, 1421 Person: 1004, 1422 Person: 1005, 1427 Bus arrived at 1430 Waiting time of 1001: 15 Waiting time of 1002: 11 2 people boarded the bus Next person in the queue: 1003 Bus arrived at 1440 Waiting time of 1003: 19 Waiting time of 1004: 18 Waiting time of 1005: 13 3 people boarded the bus No body waiting in the bus queue

10 IIT Bombay Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay10


Download ppt "IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering."

Similar presentations


Ads by Google