 Welcome to the second topic of this course on data structures and algorithms. In this week, we are going to discuss the stack class and its usage in various computational problems. The stack class is available as a part of the standard template library. The stack essentially as you all recall, implements a last in first out kind of structure which has push and pop functions both of which operate at the top of the stack. The stack of a STL is essentially a container adapter. It is created using existing container. Note that we must use include stack at the beginning of the program in order to use the stack class and its member functions. Let us look at the member functions which are available. Empty is actually a questioning function. Is the stack empty? It will return to our false size will return the size of the stack currently. Top will permit us to examine the top element of the stack. Push and pop are familiar. A push pushes an element on top of the stack and pop removes the element from top of the stack. There is a special function called swap. This is used to swap the contents of two different stacks. We will have an example today to look at this functionality. So to recapitulate the built-in member functions of the stack class are empty, size, top, push and pop and a special function swap. Let us look at the basic concept of the stack on how the stack is used. We define our program saying include stack as was mentioned earlier. Please look at the main program. It starts with declaration of a stack variable. So stack char s1 defines a stack of type char. Please remember that the stack elements must be of identical type and that type is mentioned here. So it could be char in float. In fact you could even have a struct type so that the stack could be of structures. In this particular example, we just want to demonstrate the use of stack by pushing three consecutive characters a, b and c on a stack. This program will return at the end. So this is a simple program explaining the basic concepts of a stack. Let us look at how this is achieved. So this is our program. As mentioned earlier, stack char s1 defines an empty stack of type char. I am declaring an integer variable i and a char element which is initialized to small a. Now if s1 dot empty is true, this is a test that we will see we will have to do constantly because in an empty stack, I cannot pop anything out but I can of course push things in. If this stack is empty, stack is empty as expected is output otherwise stack is not empty is output. This is just a preliminary test. Now what we are doing is we are setting up an iteration for i equal to 0 to less than equal to 2. 0, 1 and 2 will be the three values of i. Notice what is happening in each iteration. I am saying s1 dot push element. Since the initial value of element is a, a will be pushed here. The element which is pushed will be printed out. Next I say element plus plus. Note that this will increment the ASCII code which is contained in element. Therefore, I will get successive next character. When I go to the next iteration, it will push b, in next iteration it will push c. Therefore, at the end of this iteration, I would have pushed a, b and c as three characters on the stack. The size of the stack is indicated by s1 dot size. What size should it output at this stage? Well three because there are three elements in the stack. Now let us look at examining the top and then popping out one element at a time. The top element is examined by s1 dot top. What element will be seen at this juncture? Notice that we had inserted elements a, b and c in that order. So, the top of the stack will be c and c therefore, will be printed. When I pop c will disappear and then I will go back again because the stack is not empty. Next time it will show b and it will pop of b. Third iteration, it will show a, pop of a when a is popped off there is nothing left. So, this condition will actually now become true and I will come out of the loop. Essentially what we have done is we have pushed three characters on the stack and successfully examined and removed them one by one. Of course at the end the stack should be empty if it is true we say stack is empty else stack is not empty. This is only a precautionary measure. We note that we must never do the following. Number one we should never access top when stack is empty. Number two we must not pop elements when stack is empty why because there is no element to pop. Similarly there is no element at the top when stack is empty. Why are these points important? If we are not careful then our program may return something like a segmentation form. So the stack implementation of STL expects us to be careful when the stack is empty. Also note that if we had implemented the stack ourselves without using the STL class we would have to write all member functions. This is the advantage of using STL that we get readymade functions to use. Let us now look at the spatial function that I mentioned swap function. It swaps contents of two stacks. Stacks must be of course of the same type but they can be of different sizes. So look at how swap is implemented. Here as an example we are defining two stacks S1 and S2 initially all both of them will be empty. Notice what I am doing I am setting up an iteration to insert an integer value i in S1 successively. So this will push 0, 1 and 2. The next iteration on to the other stack pushes value 0, 1, 2, 3, 4, 5. Notice that S1 now has a size 3, S2 has a size 6. These are two different stacks each of the type integers. The size of the stacks displayed here will be 3 and 6 respectively. Now look at this important function swap S1, S2. It will simply take all elements of S2 and put them into stack S1 and it will take all elements of S1 and push them into stack S2. This is a massive swap which automatically occurs because of this function. Now if I test the size of the stacks I will actually get S1 dot size as 6 and S2 dot size as 3. It is a very useful function we shall have some exercises later to demonstrate how we can use this. For the time being we note that if the function swap is not available in STL we would end up writing a whole large program to achieve this swap. As a practice problem I suggest that you try to write your own swap function assume that it is not available in STL and test the above program using that swap function. So this is an exercise which I request you to try out. For your benefit these are two important references. The first one defines the stack as an abstract data type and the second one gives further details about the stack. In this particular session we have seen the basic implementation of stack available in the standard template library. All the basic functions are available plus a special function swap. In subsequent sessions we shall look at applications of stack in various problems. Thank you.