 Hello everyone, I am Mrs. Meenakshi Shrigandhi, Assistant Professor from Walton Institute of Technology, Shalapur. Welcome to the video lecture on stack using linked list. Learning outcome, at the end of this session, students will be able to implement stack using linked list using C programming. Let us see definition of stack. Stack is a linear data structure where insertion and deletion takes place at only one end called as the top end and you can see that here the top is the pointer from where insertion as well as deletion of the elements takes place. Stack follows a LEFO structure that is the last element which is added to the stack is the first element which is removed from the stack that is why stack is also called as last and first out structure. Let us see examples of stack. There are many examples in real life but here some examples are presented here. So a stack of plates and restorers if any user if he wants to place a plate on the stack it is placed on the top of the stack and if any user wants to remove a plate it is removed from the top of the stack. So we can see that the stack follows a last and first out structure that is the last element which is placed is removed at the first place. Stack of coins one more example contains stack of books. So all this follows a LEFO structure. Operations of stack, a push operation. Push operation is nothing but to add an element at the top of the stack. So here you can see that these are the elements which are already present on the stack and if I want to add any element to the stack it is added at the top. Pop operation, pop means to remove an element from the top of the stack. So if I want to remove any element from the stack it is again removed from the pointed top. Implementation of stack, a stack can be implemented using two ways that is stack using array and stack using linked list. In this video we are going to show the implementation of stack using linked list. Stack using linked list. Stack using array as limitation of fixed size that is if I want to grow the size of the stack that is not possible by using stack using array. Stack using linked list can have any number of elements. In linked list representation of stack every node represents a single element. And each node in the linked list contains two parts that is info part or data and the second part is the address part which is the address to the next node. The head pointer of the linked list is used as a top of the stack. As we have already know that the any operation that is push operation and pop operation for the stack is done from the top. So in the linked list representation of stack, the head pointer of linked list is used as the top of the stack. Let us see the illustration for push operation using stack using linked list. Initially a linked list which is representing a stack is empty. The pointer is LL and our task is to add this new node to the stack. That is I want to push this element to the stack. Now let us rename this pointer LL to top so that top will indicate the top of the stack. Now if I want to insert this new node to the stack, I have to mark top to the new node. So top is pointed to this new node and next of top is marked as null. So now this element is added as the first node of the stack. In the second operation, if I want to add one more element to this stack, which is pointed to by the pointer new, if I want to add this to the stack, next of new should be pointed to top. If I'll do this, you can see that the new node has been added at the beginning of the linked list, but you can see that top is still pointing to 30. Now I have to bring top to the new node. That is top is pointing to the first node of the stack. This is after insertion of the new node to the stack, that is after pushing the element to the stack. Suppose these are the existing element of the stack and top is pointing to element 40. And I want to add a new node to this existing stack. For that the next of new should be set to top. So after doing this we can see that new node has been added at the beginning of the linked list, but top is still pointing to 40. So I have to set the top pointer to the new node. So the top is now pointing to the new node that is with value 50. So this is after insertion that is after pushing the element to the stack. Try to think an answer. What is the algorithm for push operation? Pause the video for some time and note down the answer in your book. Algorithm for push operation. So first memory allocation of new node with pointer new should be done. And after that we need to set the data part of the new node with some value. After that we need to check whether top is pointing to null or not. It indicates whether stack is empty or not. If stack is empty then we need to set the top pointer equal to the new node. So this new node becomes the top of the stack. After that the next part of the top should be set to null. Else, else indicates top is not equal to null. This indicates that stack is not empty. So in that case the next part of the new node should be set to top. And the pointer top should be set equal to the new node. Let us see the illustration for pop operation. Now consider if the stack is empty there are no nodes to pop. So in that case we need to print that the stack is empty. But consider if the stack is not empty and I want to pop one element from the stack. So I have to set a pointer pop to the top node. And I have to move top to the next node. So after this operation top is now moved to the next node. And I have to free this node which is pointed by pop. So this element will be freed and this node is the popped element. So we can see that after pop operation this element has been removed. And top is now having the topmost element with value 50. Now consider this linked list where the topmost element is pointed by the pointed top with value 50. And I want to pop one element from the stack. So I have setting a pointer pop to the first element where top is pointing. And I have to move top to the next node. So top is moved to the next node. And this first node which is pointed by the pointer pop should be freed. So after freeing you can see that this is the popped element. And this is the updated stack where top is pointing to the element 40. Try to think and answer what is the algorithm for pop operation. Pause the video for some time and note down the answer in your book. Algorithm for pop operation. First check if top is equal to null that the stack is empty or not we need to check. If stack is empty only we need to print that stack is under flown and we need to return back. L syndicate stack is not empty and we want to pop one element from the stack. In that case I need to set the pointer pop to the top element. And set pointer top equal to the next part of the top. And after that we need to free the pointer pop. Let us see the implementation of stack using linked list by using C programming. So at the very beginning we are declaring the structure node where it is having two parts that is the data part which is the integer part. And the next part which is holding the address of the next node. So it's a pointer, pointer of type struct node. After this we are going to see the module of push operation. So here push function is used to add an element to stack. So in this first at the very beginning we are setting the pointer for the new node here a variable X has been declared. And by using this we are allocating the memory for the new node. So by using struct node malloc size of struct node here we are reserving the memory for the new node. After that we are taking one element from the user as a user to enter an element. And that is taken in X. And this new element X is assigned to the data part of the new node. And the next part of the new node is assigned to NUC. After this we are checking whether top is equal to null. So this indicates that we are checking if stack is empty. If stack is empty the new node becomes the topmost element of the stack. So in that case we are setting the top pointer to new node. And after that we are returning back. Else indicates the stack is not empty. Then we need to insert the new node to the top of the stack. So in that case the next of new is pointed to top and top is equal to new. So this was about the push function. This is the pop function. This pop function is used to remove an element from the stack. For that for this we need a integer variable X. We need a temporary pointer of type struct node. First we are checking whether top is equal to null. This indicates that the stack is empty. If stack is empty then there are no nodes to pop. In that case we need to print the message that stack is empty and we need to return back. Else indicates stack is not empty. If stack is not empty then in that case we can pop the elements from the stack. So for that a temporary pointer is set to top. But temp is also pointing to the top element. And top is moved to the next node. And where temp is pointing its data part is now collected in X. So this is the element at the top of the stack. So the popped element is printed here. And after that we are freeing the topmost element that is pointed by temp. So this was the pop function. After that we have a display function. The display function is used to display all the elements of the stack. For that we need a temporary pointer variable to traverse the link list. And we need to set the temporary pointer to the top of the stack. And we need to traverse till temp is not equal to null. So we are using a while loop to traverse the link list till null is not reached. With each iteration we are printing temp of data. Means whatever temp is pointing this data part is printed. And after each print we are moving temp to the next node. So this loop is repeated till temp is not reached to null. So in this way it will print all the nodes which are present in the stack. So this is a count function. The count function is used to count the number of elements of the stack. For that we need a temporary pointer variable which is initialized to top. And we need a variable count which is initialized to zero. And by using a while loop we are traversing the link list till null is not reached. So every time with each iteration the count variable is incremented by one. And after that the temp is moved to the next node. So this is done till temp is not equal to null. After that we have a main module where we are initializing the top of the stack to null. And we need a integer variable ch to take the choice from the user. In a do-by-loop we are taking continuous choices from the user. And with each choice in the switch case one of the cases is executed. The cases are in this way. The case one is for push. Case two is for pop. Case three is for display. And case four is for count. So one of the choices is executed. This is in a loop till ch is not equal to five. So it is taking continuously the choices from the user till the user doesn't press this five. So this was implementation of stack using link list. These are my references. Thank you.