 Welcome to this course on data structures and algorithms. In this particular session, we will continue our discussion on implementing a bus queue. We have already seen the demonstration of a simple bus queue. We will recall that in this example, we had assigned arbitrary timings for people joining the bus queue, arbitrary timings for buses to arrive and then people leaving the queue and joining the bus. In this particular session, we will look at a computer program which implements that particular simulation of a bus queue. First, we discuss the data structures that we require to simulate the bus queue. As agreed, we will use queues to represent arrival of passengers. So, each passenger is given a unique ID. This is an artificial ID and this helps us to identify the person who leaves and calculate the corresponding waiting time, etcetera. Each person is also assigned an arrival time. Again, for now, it is done arbitrarily. So, we store this information in a structure called personal info which contains the people, ID and arrival time that arrive and stand in the queue. Next, we require a queue which we call the bus queue. Now, our queue is of the type structured personal info. Why? Because each person will be standing in the queue and each person is represented by an artificial ID and an arbitrary arrival time. So, the queue will be of type structure. It contains information about all the people as above. Let us look at the additional functions that we have to write now. Please note, these are above, over and above the functions which are already available as standard template library queue functions. Now, the functions that we will need would be required to handle a, the arrival of a person and b, the arrival of a bus. Person arrives, it is a very simple function. All that we need to do is push the information of the person on the queue that is bus queue which includes person, ID and arrival time. Boarding the bus is slightly more complex. We will have to handle in this function the arrival of the bus. Then, depending upon the available seats in the bus, people in the bus queue will board the bus. We will calculate the wait time for each person and we will note that if the bus arrives earlier and has not departed, when a person arrives, of course, the wait time is 0 for that person. Now, these two simple functions we will have to write in addition to using the standard queue functions. So, let us look at the implementation of this bus queue. First, we define the struct personal info. Person info is very simple, person, ID and arrival time. We note that the person ID will be arbitrarily given by adding one number in sequence for every new person arriving and arrival time will be arbitrarily decided. The class bus stop is defined with a private member data static int ID. How do we use this? This number will use as the base number to generate the sequence of person IDs as people arrive. We next define the queue, which is person info bus queue. In public members, we have the void person arrives with time as a parameter and void board bus. This has two parameters, the bus arrival time and the available seats for that particular bus. This is all there is to the definition of the class bus stop. We note that we have an int bus stop colon colon ID equal to 1000 and arbitrary number, which is used as a base to start assigning the ID numbers to passengers who arrive. Next, we have the person arrives function. So, we say void bus stop colon colon person arrives. Note that the parameter is time. So, when the given time is it comes to this function, we define the person info as a person and we assign the plus plus ID to person ID. So, if our base was 1000 for example, each person who comes in will be assigned in sequence 1000, 1000, 1000, 2000, 3000, 3000, 4000 as arbitrary ID. The time which is passed on to this function is the arrival time and that we assign to the person dot arrival time. We know what to do now with this information. We have to push this person on to the queue, which means we have to push the information about person ID and arrival time. All that we need to do is we say push person. We will just print out information about that person is a person ID and the arrival time. This is a simple function that we need. Next is a more complex function where we implement the boarding of the bus. You will recall our discussion on various things that need to be handled when somebody boards the bus. Please note that this particular function has two parameters. One is the bus arrival time and the other is the number of available seats in that bus. We initialize some variables at the beginning like count is 0, etcetera, personal arrival time is 0, etcetera. At this place, we will write the code for boarding the bus. This code is given in the next slide. Here, I am just describing the overall structure of this function board bus. So, once the code is executed for boarding the bus, we will simply output the count, the number of people who boarded the bus and if the bus queue is not empty, we will just print out the next person in the queue who is still waiting. This will be the bus queue front at this moment and the person ID. Otherwise, we will say nobody is waiting in the bus queue. So, this is the function. Of course, we still have to write the code for boarding the bus, which is explained here. Now, there are people who are waiting in the queue and there are certain number of available seats. The logic that we use is, we run an iteration for i equal to 1 to the number of available seats, 2, 3, 5, 8, whatever it is. Now, at this, at every iteration, we will check if the bus queue is not empty. That means, there is a person waiting. If there is a person waiting, we note the person arrival time from the bus queue front dot arrival time. If the arrival time is of the bus is less than personal arrival time, we know that there is no waiting time. But, otherwise, the waiting time is calculated as bus arrival time minus person arrival time. We output the waiting time of that person by printing the bus queue dot front and then, we output the waiting time. We increment the count and we do not forget to pop the top person from the queue. Why? Because the person has already bolded the bus. So, please note how simple it is. We run an iteration for 1 to available seats. If the number of available seats are more than the people in the queue, everybody will be accommodated. If not, some people will still be left in the queue. This completes the simulation for boarding bus. Our main program is very simple. We define bus stop as a B stop, a variable in. So, this is an instance of the class bus stop. We define bus arrival time. Now, look at our arbitrary assignments. I repeat again that in actual simulation, the person's arrival time will have to be randomly generated. But, to be consistent with the illustration that we had given in the last session, we just arbitrarily assign value 14, 15, 14, 19, 14, 21, 14, 22, 14, 27 as the arrival time of different persons. Since we had shown 5 people arriving, we call this function 5 times. Next, we know that two buses arrive in our illustration. So, we put the first bus arrival time as 1430. We print that information and then we call the board bus function. We note that when the board bus function is executed, since there are only two seats available, only two of the five people in the queue will board that bus. The next bus arrives at 1440 as per our illustration. So, we print again the bus arrival time. This time there are seven seats and as we noted in the illustration, everybody in the queue will be accommodated. This is the end of the main project. This is the sample output that you will get. Person number 1001 arrived at 1450 etcetera, 1005 arrived at 1427. The first bus arrived at 1430. Waiting time of individuals was this much, two people boarded. Next person in the queue was 1003. Next bus arrived at 1450 and then their waiting time, three people boarded the bus, nobody waiting. So, you see it is extremely simple to use STL queues to simulate real life queues. Of course, as I mentioned in real life, we will not be able to give arbitrary arrival numbers, arbitrary number of seats available in the bus and this I once again request you to look at the practice problems that we will be putting up shortly, so that you could solve them. We will eventually put the solutions of these practice problems also on the IIT Bombay. To conclude in this session, we looked at a C++ program which implements our illustrative example of simulating a bus queue. We also note that in this illustration, we have used arbitrary timings of arrival of people and arbitrary timings of arrival of bus and arbitrary number of seats available. In real life, these will all be replaced by generating appropriate random numbers representing what we call the probability distribution of the arrival of people and arrival of buses. Thank you very much.