 Welcome to this course on data structures and algorithms. Today we will be discussing implementation of queues using linkless. This is last of the three sessions which describe the implementation of queues. Last time we looked at the implementation of queues using fixed arrays. Now we will look at the linkless implementing. We will use a node to represent an element of the queue. So, the structure of the node is very simple. It will contain information and will contain a pointer to some other node. We shall see how the pointers are used. Our queue itself will be a linked list of nodes very similar to what we did in implementing stacks using linkless. There is no fundamental difference in these two operations. We revisit the functions that we will have to write to initialize the queue, to test the status of the queues whether queue is empty or full. Notice that full is not required in this case because we are using dynamic allocation of memory and we presume that memory is always sufficient to handle as many elements as we wish to insert in the queue. Inspecting front which is similar to the top of a stack. We will need a function to insert an element in the queue. This is a bit tricky because insertion must logically happen at the end of the queue. We will see how it is managed just as we managed it in the case of arrays. Front of course is similar to pop-off stack. Let us look at these functions now. The structure of the node is defined very simply. We have already seen that info and star next are the two items in that structure. The queue itself is defined as a linked list of these nodes. Initialization of the queue is very simple. We simply set front to null that means queue has nobody. Determine whether queue is empty or not. Again if queue front is null, it is empty otherwise it is not empty. We have not written any function to examine whether the queue is full or not. As mentioned, we believe we do not need one while implementing it using linked list. This exam is the front element of the queue. Now, let us say this is the front element of the queue. I want to examine means I want to get the value f without changing the status of the queue. Very simply I examine whether the queue is empty. If it is empty there is no element in the front. If it is not empty, whatever node is pointed to by front is the front node. So, I just look at that info, return it into the value of element as my return value. And that is how the inspection is completed. Notice that the status of the queue is not changed at all. Now comes the major operation of inserting an element in the queue. We assume that an element f is to be inserted and we assume of course that we are starting afresh that queue currently does not have anything right now. So, we define a node star temp and allocate memory to it. When we allocate memory to it a node gets created which can contain some information in a pointer. Next the value that is to be inserted is assigned to temp info. That means here I will have f as the value and we still do not know what this pointer is. But notice that this is the only element in the queue. It does not point to anybody else. So, this will have to become null further. The front of the queue has to point to this node itself. We are complete this by setting temp next to null and queue front to temp. Very simple operation f has got inserted now. So, this is the queue front which will now point to this node and temp has now become a full-fledged node in my linked list implementation of the queue. Consider I wish to insert an element when the queue is not empty. That is when I will come to the else part. That means it already has one or more elements in the queue. Now I will need because remember what I said earlier will have to actually push the new elements in the end of the queue. But in the case of arrays I had to keep moving the elements earlier forward so that I could make a place. Here I do not need to keep moving the elements, but I need to traverse through the linked list to find out where is the last element in the queue and then insert a new node just behind it. So, look at how we do that. I will need an additional variable which I am calling temp one which will temporarily hold the address of any node. Assume that the queue has element f and r is to be inserted. Let us see what happens. I come to this temp one. So, temp one is now created. It is pointing to queue front. That is all I know about the queue anyway. Next I execute a wild statement. Wild temp one next is not null, but in this particular case temp one next is actually null. So, I will not execute the wild statement. I will simply come over here and assign the temp to temp one dot next. Here is where the utilization of my temp one and temp comes out. Remember temp contains the new element to be inserted. Now when temp one which currently is there, I need to actually move the queue front which was f to point to r. So, notice how I am doing it. Temp is now given the value temp one next and I will return to. In the end, notice what will happen to the linked list? Queue front will point to f as is required, but now this pointer in this node will actually point to r. So, effectively the new element r has been inserted at the end of the queue as is required. Let us examine when there are more elements than one in the queue and we wish to insert an additional element. Now, we have already seen that this else part I will come to when the queue is not empty, but now we assume that the queue has element f and r already and y is to be inserted. So, what happens now? Queue front is pointing to f, f is pointing to r and r is not pointing to anything. Notice that the new node now that we will have to insert, we will have to insert it such that it is connected to this node and at this point. However, I do not know where this node is because all that I know is queue front which is pointing to this. This is where the wide statement will now come into action. First I will put of course temp one to point to queue front as we had done last time. So, this will point to the queue front which is currently f. Now, I will execute the wide statement and this wide statement will ensure that the temp one is now allocated to the temp next. So, that means I will move away from f and go to r. It will now point to r and now I know that this is the last element because this pointer is null which is what I am examining. Temp one next is null. If as long as it is not null, I will keep traversing. When I come across the last point, I know this is where I have to insert the new element. All that I do is I set temp one next to temp and return true. That means this temp now which was y will now get added in this fashion. Queue front will point to f, f will point to r, r will now point to y and y will point to nothing. This is how my entire queue is created. So, very simple and straight forward. Just note one fundamental difference in case of array. We kept pushing elements to make place at the end of the queue. In case of a link list, we merely traverse the link list to go to the last element and attach the new node to the last element. Removing an element from the queue is very straight forward. Whatever queue front points to is the element that needs to be removed. Of course, that assume that the queue has f and r as two elements. When I remove f, queue front must point to r. It is a very simple operation. So, what I do is I of course, if queue is empty, I do nothing. But if queue is not empty, I assign temp to queue front which is temporarily holding a pointer here. Next, I allocate queue front next to queue front. That means, I am moving this pointer here to the queue front to point to this r. Now, this element is the one which I am returning and therefore, I say element is equal to temp info because temp info is pointing to this f. Having done that, I free temp so that my entire temporary storage is released. That means, this node f is released and queue front now points to r. So, the queue is now continuing with the remaining element. Having seen all of this, it is very simple to write a main program to illustrate these activities. I will not spend much time on that because this program is written exactly like the previous program when we implemented queue using arrays. There is absolutely no change and the functions will behave slightly differently because they are handling link list rather than arrays. But the main program will work exactly like that and I will insert an element f. I will insert say two more elements r and y. If I try to insert any additional element, it will get inserted. So, we have removed that part of that main program now. To access front element, this is the illustration and to remove all elements from the queue, this is the code again exactly same as the past code. You can run this program and you will see that the output that you get is very similar to the output that you got when you implemented the queues using arrays. In short, we have seen the implementation of both stacks and queues using fixed memory structures such as arrays as well as dynamic structures as link list. Thank you so much.