 Welcome to this course on data structures and algorithms. In this session, we are going to look at queues. We will see how they are implemented using the standard template library of C++. There is a queue class. Please remember that queue represents first in, first out. We are familiar with queues. So, we know that a new person entering a queue will always enter at the back and the person leaving the queue will always leave from the front. And therefore, we have to push at back and pop from front. This is different from a stack where both push and pop occur at the same end. For queue class, there is a container adapter which is created using existing containers. Note that we must use include queue at the beginning of our program. The member functions of the queue class are functions with which we are familiar. Empty, this will test whether the queue is empty. Size, it will tell us the size of the queue at any point in time. We have now front and back. Remember in stacks, we had only the top. This time we have facilities to examine the front element or the back element. There are two functions to operate on queue. Push, as we said, the push will always occur at the back and pop which will always occur at the front. Finally, there is a swap function very similar to the swap function for stacks. Let us see an example. Here is a program which illustrates basic concepts of the queue. So, we have include queue as I mentioned. I declare queue of characters, say queue 1. Now, we will just try and push three arbitrary characters a, b and c on a queue. This is the main program. I declare queue as queue 1. I initialize an index variable i and the care element initially is a. Please note that like stack, I also check whether queue is empty. If queue is empty, we will say queue is empty as expected. Otherwise, if queue is not empty, we will state that queue is not empty. Now, we set up an iteration for i equal to 0 to i less than equal to 2 and we push the element into the queue. Please note that the first time the element that will be pushed will be the character a. Immediately thereafter, we increment element. We note that the ASCII value of a is incremented giving us the character b and subsequently character c. So, you get success in next characters. Finally, we output the size of the queue which obviously would be 3. We continue looking at this while the queue is not empty, we will output the front element that is queue 1.front. The back element is queue 1.back. We will pop the front element. Please note that queue 1.front only permits us to examine the front element. To remove it from the queue, we have to use pop. Now, this will be done repeatedly till the queue 1 turns out to be empty. If queue 1 is empty, indeed we will say queue is empty, otherwise we will say queue is not empty. Please remember just like in the case of stacks, accessing front or back when queue is empty is to be avoided because in an empty queue, there is no element at the front, there is no element at the back. In fact, it could give rise to a segmentation fault in our program and that is why we have to be careful and test the queues, emptiness or otherwise every time we operate upon the queue. Similarly, we should not pop elements when queue is empty. These are simple precautions that we need to take and these can be taken. There is a swap function which is a more complex function. It swaps contents of two queues. The queues must be of the same type. The sizes may be different. Please note that this swap function is exactly like the swap function that we had for stacks. So, here is the main program. We push a integer element i into queue 1. We push an integer element i into queue 2, but this loop runs from 0 to 2. This loop runs from 0 to 5. That means, this has three elements, this has six elements. So, the sizes are different. However, we just print sizes by the way for our information. The sizes of queues are this. Now, when we say swap queue 1, queue 2, the two queues will be swapped. So, now if you print the sizes of the queue, you will see that queue 1 dot size will be now 6 and queue 2 dot size will be now 3 because we have swapped the two queues. This is just an illustration of the simplicity provided by the swap function. As a practice problem, just like I requested you to try it out for stacks, assume that the function swap is not available in STF. Can you write your own swap function to provide the same process of swapping two queues? Test the above program using that function. So, use your own swap function and test this problem. Here are a few references. One is from Wikipedia on the queue and the other is C++ tutorial. Both of them represent open source material. So, you can easily download them and read them whenever you want. To conclude, in this session, we have looked at implementing queues using standard template library. We have seen that queues are very similar to stack except that we have a front and a back for a queue as opposed to only the top element of a stack. We have seen how we can operate upon the queue and we have also seen how to use the swap function for swapping two queues. In the next session, we will take an illustrative example and we will try to implement that using the queues and the standard template library. Thank you.