 Welcome to this course on data structures and algorithms. In this last session of the three sessions on stacks, we shall look at implementation of stacks using linked list. So here we go. First we will use a node to represent the element of a stack. We define the structure of the node as care info and struct node star next. So this is how a node will look like. It will have information and a pointer to the next. We now need to create many such nodes which will form the stack. So the stack is simply defined as a structure which has node pointer. So it will be a linked list of nodes. We recall that for implementing stacks we require to write, we are required to write several functions, initialize to examine whether the stack is empty. Note that we will not require a function to check whether the stack is full. Why? Because now we are using a linked list and dynamic allocation. We presume that we always have large enough memory so that whatever number of elements we push on the stack, the stack will never be full. Like last time, we need to examine the top element. So we need a function for that and of course the two operational functions push and pop for the stack. So let us look at these functions. First definition of structure of a node, we have already seen that. Definition of structure of stack, we have already seen that. It is a linked list of the nodes that we have defined here. Initialization of the stack is very simple. We just set the top pointer to null. That means the stack does not contain any node at all. To determine whether stack is empty or not is also equally simple. If the top points to null, then the stack is empty. That is what we check and return. How do we examine the top element of the stack? Again simple. Suppose the stack has one element f, we check in this function. If stack is empty, we return false. That means there is no top element. Otherwise the element is returned as s top and the info path. So this is the node which is pointed to by s top. The information it contains is f and that is what will be returned in the element. And of course the function will return true because it has found one element at the top. Now comes the main path. How do we push or insert elements on the stack? Consider that we want to push one element which is value f in the stack. So we write bool stack push as a structure node star temp. We allocate memory to that temp and we insert the value into temp info as it is shown here. This is the temp node which is created and inside this temp node we insert f. Please note that what we have done is we have just prepared the background for inserting the node in the stack. What we have done is we have created a node with the requisite information. How does it get inserted into the stack? Let us see the subsequent part of this function. First if s of top is not equal to null. That means when the stack is not empty we will have to do so many things. We will examine these later. But what if stack is empty? Well when stack is empty it means that this is going to be our first node. So we jump to this side. This occurs when stack is empty and it will occur in the else path of this if statement. What do we do here? Very simple. We have the temp node which contains the information f. It has some garbage in the pointer path. We first make s top to point to this node. So temp is assigned to s top. Second the pointer in the node itself must be null. So it is set to null here. That is it. Now this becomes my stack s top points to this node and this node in turn does not point to anything. So now my stack has one element. Now let us come to this part when the stack is not empty. So we have just seen that it now has one element. It will examine that this stack is not empty and therefore it will come here. Please note that this time the temp which we would have created would have another element. Let us assume that it is r. So now we have a new element r to be put onto the stack and it has been created by this appropriate operation namely r has been inserted there and currently this does not have any particular value in the address path. But s top is pointing as we had seen earlier to this node which contains f. What that we need to do is we need to point s top to this and we need to insert a pointer here which will point to f. The process to do that is very simple. Temp next is assigned to s top first. So notice this temp. This temp next in it is inserted the address of s top so that indirectly it points now to f. We have now achieved that this node points to this. However s top must point to this in turn. So we set s top to temp which is the temporary node that we have created. Notice now how the stack looks like. s top points to this new node that we have inserted and this new node in turn points to an earlier node. Therefore a linked list of r followed by f gets created and that is how our stack will look like. So conceptually and for implementation both are extremely simple and straightforward things here. Let us look at popping an element from the stack. For this purpose we assume that the stack currently contains r and f. We create the temp node as earlier. If stack is empty we will return false of course. Otherwise we will now make this temp point to top. So notice that temp is being assigned s top that means temp points to r. We want to pop off this element and now temp points to this and therefore the element is temp info which is this information. Notice what is happening here. Temp is now pointing to this. Temp info is r. r is being given out as the value of the element but that is good but not good enough. I now must point s top to the remaining element here or the next element in the stack. So I first free temp that means I get rid of this completely and s top is made to point to s top next which is pointing to this. I will free temp and return true. So this might appear slightly contrived. Let us just go through the actual mechanism. I still use a temp which is merely as a place holder really. It is initially made to point to s top because it is pointing to r. s top now is made to point to f. This is the element which is to be popped off. So temp info r is returned as the element and temp is free which eliminates this element from the stack. Now you will notice that having implemented these functions using linkless the main program hardly changes from the one that we used for implementing stacks using arrays. So I have stack s1 care element. I initialize the stack exactly in the same fashion. I confirm whether stack is empty or not exactly in the same fashion. Next comes pushing an element f. If stack push and s1 element is true then print that element is pushed in otherwise error. Notice that there is no difference whatsoever in this main program and the one that we wrote earlier because the functions are implementing the operational hazards which are different for linkless and different for arrays. We similarly push two more elements say r and y in the stack. We continue with our main program to access the top element. We use exactly the same code and it will return the top element to us. We pop all elements from the stack again and artificial iteration running up to ten elements is written here. We know that the stack does not have ten elements. It has three elements but operation stack pop will actually push the first element in the next iteration, the next element, third element and so on. When the stack is empty it will say cannot pop and come out. In short the output that you will see will be exactly identical to the output that you saw in the case when we implemented the stack using arrays. These are just two ways of implementing stacks that we have discussed. There could be refinements and modifications here and there could be different styles in writing the same code having the same effect but I hope you have understood how easy it is to implement stacks using conventional procedural programming in C++ by using either arrays or by using linkless. Thank you so much.