 Hello everyone, welcome to the video lecture on stack using array. I am Mrs. Meenakshi Shrigandhi from Volchin Institute of Technology, Sholapur. Learning outcome? At the end of this session, students will be able to implement stack using array using C programming. Definition of stack? Stack is a linear data structure where insertion and deletion takes place at one end called as top. Stack is called as a leaf-all list as the item which is inserted first is the first to be removed. That's why it is called as last in first out list. Let's see the examples of stack. There are many real-time examples of stack but here only the focus is only on three examples. The best example for a stack is the stack of plates in a restaurant where a plate is inserted at the top of the stack of plates and if anybody takes a plate from the stack of plates it takes it from the top. The next example is the stack of coins where a new coin is inserted at the top and a coin is withdrawn from the top of the stack of coins. The next is stack of books where a new book is placed at the top of stack of books and a book can be withdrawn from the top of the stack of books. Let's see the operations on stack. There are mainly two operations on stack that is push and pop operation. First we'll discuss the push operation. Push is nothing but a process of adding an element at the top of the stack. Here you can see these are the elements of the stack. If a new element to be added we can push at the top of the stack. The next is the pop operation. Pop is the process to remove an element from the top of the stack. Here it shows that the element F has been popped from the top of the stack. Let's see the algorithm for the push operation. The first step is to initialize the top. Here we need to initialize the top to its proper position so that the elements are added from the proper place. The next step is to check for the stack overflow. Stack overflow means here we are checking whether the stack is full or not. Stack is full means all the locations of the stack are filled. If all the locations of stack are filled then the user cannot insert any element to the stack. So we need to check for stack overflow before we add any new element to the stack. If the stack is not overflowed then we need to increment the top to the next place and add the element to the stack. Let's see the illustration for push operation. These are the initial conditions of an array of size 5. This is representing a stack. Here top has been initialized to minus 1. For push turn operation first we need to increment top by 1. Now top is pointing to the 0th location then add the new element 10 to it. For push 20 initially the top is pointing to the 0th location first we need to increment the top to the next location now add the element 20 to it. For push 40 first the top should be incremented to the next location that is the third location now add the element 40 to it. For push 50 initially the top is pointing to the third location now for pushing the element 50 first we need to increment the top to the next location that is location 4 now add the element 50 to it. Now push 60 now here it is not possible as the stack is full the stack is overflowing. Now the condition to check whether the stack is overflowing is not that is when top is pointing to size minus 1 it indicates that the stack is overflowing. When it is overflowing we cannot add any more elements to the stack. Now think and answer based on the algorithm for push operation try to think and answer the algorithm for pop operation. Pop operation is nothing but the process of removing an element from the top of the stack. Let's see the algorithm for that first we need to check whether the stack has under flown or not. Here stack under flow means to check whether the stack is empty or not. If the stack is empty we cannot pop any element from the stack. So before popping any element from the stack we need to check whether there are any elements in the stack or not. If the stack is not under flown then remove the element from the stack where the top is pointing then decrement the top. The pop operation can be best explained by using the illustration. So let's see that here is the initial conditions of the stack. These are the elements of the stack where top is pointing to the location fourth and for pop operation initially the top is pointing to the fourth location. For pop operation first where top is pointing that element is removed then top is decremented by one location so the popped element is 50. Next pop operation here initially top is pointing to the third location. So first pop that element then decrement the top by one location. Now the popped element is 40. For next pop operation top is pointing to the first location first pop that element where top is pointing then decrement the top by one location. So you can see that the popped element is 20. For next pop operation initially top is pointing to the zeroth location where top is pointing first pop that element and decrement the top by one location. So here after decrementing top is pointing to minus one location. So as you can see the popped element is 10. Now next pop operation cannot work as the stack has been under flown. The condition to check whether the stack has been under flown is not is top is equal to minus one. Let us implement the stack using array by using c programming. First we'll focus on the main module. First declare all the header files and we have as defined the size to five. There is an array integer array of size five and an integer variable top. These are the global variables. Now push pop and display these are the functions whose prototype has been declared here. In main function first the variable integer variable ch has been declared to take the choice from the user. Your top has been initialized to minus one and by using do while we are continuously taking the choices from the user. First we are taking the choice from the user based on the choice by using the switch we are executing one of the function. So when the choice is one then the push function is executed and when the choice is two the pop function is executed when the choice is three then the display function is executed and when the choice is four the program is exit. Let's see how to push an element to the stack. First we are declaring an integer variable x to take an element from the user. As we have seen in the algorithm the very first step is to initialize the top that we have already done in the main. So the second step is to check whether the stack has over flown or not. So the condition to check if the stack has been over flown or not is top is pointing to size minus one. So if top is pointing to size minus one that indicates that the stack has been over flown and if that is the case we need to return back to main. Else indicates that the stack has not been over flown. So in that case we can take an element from the user and increment the top by one location. Now add the element to the stack where top is pointing. So this is the push operation. Now similarly we can write the pop operation also. So first we are declaring an integer variable x so that the pop element is discrete. So in the pop algorithm we have already seen that the first step is to check whether the stack has under flown or not. So to check whether the stack has under flown or not is done by using whether top is pointing to minus one or not. If it is pointing to minus one it indicates that stack has been under flown. If that is the case we need to return back to the main function. If stack has not been under flown then the element where the top is pointing that element is remote and it is placed in x and top is decremented by one location and then the popped element is displayed. So this is how the pop operation works. Finally we are going to display the elements of the stack. So for that first we are declaring a variable i. Before displaying any elements from the stack we need to check whether the stack has been under flown or not. If it is under flown then we cannot display any elements. So to check it is under flown or not that is done by using whether top is pointing to minus one. If it is pointing to minus one it indicates that stack has been over flown. If that is the case we need to return back to the main function. Else if stack has not been over flown then we are using a for loop and we are displaying the elements of the stack. This is how we are implementing stack using array by using c programming. These are my references. Thank you.