 Welcome to this course on data structures and algorithms as mentioned last time we will be discussing the implementation of a stack using a fixed size array. Note that when we use a fixed size array the stack will have a limitation of size. To illustrate we shall use a small fixed size so I define max size as 3 and then I define the stack structure as a character array A of max size which will contain all my elements and in size is the variable representing the growing size at any point in time. I define the type depth stack as stack so that stack becomes a defined structure. Please note that max size is defined as 3 so that it is easy to illustrate using this example. We recall that we had observed that whenever we are implementing a stack first we need to define and initialize it then we must have two functions to test whether the stack is empty or stack is full. Finally we also need an examination of the top element at any point in time so we need a function called top and of course the main operational functions of stack for pushing an element and for popping an element. We shall now see implementation of these functions in C++. First the definition of structure of the stack so this we have already seen this structure is defined initialization of stack is very simple. I merely said the size of stack to 0 whether the stack is empty or full can be determined by these two functions note that these are Boolean functions stack empty and stack full. It will return if size equal to 0 that means the stack is empty it will return true if size equal to max size if stack is full they are very simple to understand. Let us look at the implementation of a function which examines that top element of the stack. For example that stack has just one value f as shown here the Boolean function stack top which has two parameters stack star s and care and element so this is the element which will be written and that is why it is being written by reference. If stack is empty of course I will return false that means the stack top there is nothing to examine else element is set to s of size minus 1 by size minus 1 please note that if there is only one element which is at the 0th position of the array size will be equal to 1 and since the element is at f I have to return that element I will return true. Note that size does not change by this function because this function merely examines the top element does not change the status of the stack at all. Let us now look at push and pop operations which are the main operations of the stack. Note that these are implemented as Boolean operations are they Boolean operations well no because when I push I want to push an element on the stack and when I pop I want to get back an element. However whether the operation is successful or not is equally important to be known and that is why these functions have been implemented as Boolean functions. So the function itself will return true or false depending upon whether the operation is successful or not the element in question is passed through another parameter. So for example for pushing the care element will carry the element that is to be pushed. Imagine that we need to push R on the stack which already contains f as we had seen earlier. The implementation is very simple if stack is full we simply return false without it pushing anything because nothing can be pushed otherwise we push the element on to the stack at S size and then we say size plus plus so that size is incremented for subsequent usage. So notice that size was one originally that is where the value R will be inserted and size will be changed by size plus plus making a new size two. Let us look at the pop imagine that R was at the top of the stack the element which will be pop will be returned through the pointer and element. The function itself is implemented as Boolean as mentioned earlier so that we know whether the operation was successful or not assuming that the value R is at the top of the stack as shown here if stack is empty which is not true in this case but if it were this function will return false otherwise it will first reduce the size. Please remember that the size which was too earlier has to become one now. The element R which is at the top is at the position one and that is what is returned here by this element it will return true because indeed I have been able to obtain an element from the stack. So you see the pop and push operations are extremely simple and straightforward. Let us now illustrate the use of a stack through a program which will push multiple elements and pop them out one after another. So here is the main program I define stack S1 please remember the structure has already been defined. I define care element which will represent an individual element to be pushed or popped I will initialize the stack in it. I will confirm that it is empty note that it is not required to be done here because the stack is empty indeed because we are starting the program but this is just an illustration. So if stack is empty it returns true then stack is empty as expected is what we print otherwise we print an error stack is not empty. Of course we would expect that when the main program is executed we will get and message stack is empty as expected. Having confirmed that stack is empty and having initialized the stack we will now start pushing elements. Next in our main program we will push elements one by one since we agreed that the first element will be F we set the value of element to F and use the stack push function. If the stack push happens to be true that means the element got pushed we just print out an appropriate message otherwise this program will print an error message. Note that since the size is 3 can accommodate up to 3 elements so F will definitely get pushed. In exactly similar fashion we push 2 more elements say r and y. So we will just repeat this code. Now that the size is full we try to push one more element say x so we will put element equal to x and try to push the stack. Note that this time it will not return true that is because there is no space in the stack and therefore it will give an error message cannot push stack is full. In short our stack now contains the elements F, r and y and that is the end of the limit of the stack. We have written a few more lines of code in this main program to illustrate other functionality for example examining the top element. So you remember stack top was the function return to examine the top element. In this particular case it will return the top element if the stack is empty as you know it will give an error message saying stack is empty. Now we wish to pop all the elements one by one from the stack. I can set up a loop of an arbitrary iteration number for example I have set up an artificial loop for int i equal to 0 to 10. Obviously the stack does not contain 10 elements but you see the whole point is I need not worry about how elements the stack has if the stack is empty it will stop popping up further element. So look at what I am doing inside this iteration simple I do a stack pop operation if it returns a true that means some element was popped in which case I print that element otherwise I say cannot pop stack is empty. Note that when the stack is empty no further elements can be popped and therefore I simply break from this loop. So this will ensure that I will pop all the elements of the stack. If you actually run this program including the additional code to insert two more elements to be put as indicated earlier you will actually get this kind of output. Finally it will print stack is empty as expected next element f will be pushed r will be pushed y will be pushed. When you try to push the element x it will say cannot push stack is full. Finally you are examining the top position of the stack which will return y. Then one after another we are popping of the elements so that loop will successively print element y pop element r pop element f pop when the loop tries to pop any more element it will say cannot pop any more. So notice that the stack implementation using an array is an extremely simple thing the functions that we have written now can be used anywhere any time if you want the element type to be different simply use a different type instead of care and you will get a stack of any particular type that you want. Note that the function definition itself will not change except for the parameter type that you will push or pop. Next time we will look at the implementation of stack using linked list. Thank you.