 Hello everyone, my name is Nikhil Gajam, working as assistant professor in department of computer science and engineering in Walton Institute of Technology, Solapur. In today's video we will see a program for displaying singly linked list in reverse order. This is a learning outcome. At the end of this session student will be able to write a C program for displaying singly linked list in reverse order. We will see first what is singly linked list. A linked list can be defined as a collection of node where each node contains two fields in general two fields. First one is actually a data part, we can use integer value as a data part. And second one is a pointer which contains address of next node in the memory. So last node of linked list contains a null indicating that this is the end of linked list. So this is general a singly linked list. These are diagrammatically representation of singly linked list. First node is actually a start. So start is a pointer, pointer to a structure which holds address of first node. So start indicating the start of singly linked list. And there are different nodes are present in singly linked list. In this diagram there are four nodes. So 10, 20, 30, 40 are a data part present in each node. Addresses we have used as 100, 200, 300 and 400. Last node's next part always contains null indicating that this is end of linked list. So two variables generally we are using important in singly linked list. First one is start indicating a start of linked list. So without start we cannot traverse a linked list. So start is a pointer to a structure. And last one last node's next part always contains null in singly linked list indicating that this is the end of singly linked list. So each node's next part which is a pointer which always holds address of next node. So 10's next node is 20. So 20 node's address is 200, 200 is stores in 10's next part. Like this why singly linked list is created. We have taken one example. Suppose there are five nodes in singly linked list 10, 20, 30, 40 and 50. Start always points to a first node. And last node's next part is null. Suppose we are finding out how a singly linked list can be displayed in forward direction. For forward direction we have taken a temp for traversing a singly linked list. Again temp is a pointer pointer to a structure. Initially temp is pointing to a start or first node. So wherever temp is pointing we are printing data part of that node. So 10 is printed. Using while loop or using any loop we can traverse the temp from first node to last node. And we go on printing the value of data part of each node. So next time temp will point to 20 and we will print 20 and then temp will move forward we will print 30. Then temp will move forward we will print 40. Again temp will move forward we will print 50. In this way we can print a singly linked list in forward direction. Now we will find out how singly linked list can be displayed in reverse direction. The general algorithm is given as traverse the linked list till the end using recursion. So using temp we can traverse till the end and then start displaying the data part from last node to first node using recursion. So there are multiple ways to print a singly linked list in reverse direction. This algorithm is using an approach of recursion for printing a data part of singly linked list in reverse order. So temp starts from start to end we cannot point temp directly to the end. So only way to traverse the singly linked list is using start. So temp point to a start node and then traverse temp till the end and then go on printing from last node to first node. So this is an algorithm for printing singly linked list in reverse order. Just pause the video and you can answer what will be the exit condition while printing in reverse direction using recursive approach or using recursion function. You can pause the video and you can answer. Answer for this is if temp reaches to start in that case you are returning or you can stopping. So when temp reaches to end we are starting printing from last node till temp reaches to start. So this is an exit condition or where we can stop using recursion function while writing a recursive function. Now we will see a program actual program for displaying a singly linked list in reverse order. This is an online compiler I am using. This is a program for singly linked list. In this program I have written two functions. First one insert at end for inserting a new node or for creating a singly linked list. And then display which will display a singly linked list in forward direction. I will execute this code and we will see whether this program is working correctly or not. So there are two choices. This is a main node driven program. Choice 1 is for insert the new node at the end and choice 2 is for displaying the singly linked list. So choice 1 I will enter. Enter data to be inserted in the singly linked list. Suppose I want to insert 10. Enter choice again 1. Suppose I want to insert 20. Next node I want to insert is 30 and next time choice 2. So here singly linked list is displayed with nodes containing 10, 20 and 30. So 30 is the last node. Every time whenever you insert the node that will be added at the end. Now I will write a program or a function for displaying this singly linked list in reverse order. So I will write or I will add a new case called as reverse display. So reverse display is my function. I am passing start, starting address, starting nodes address to this function. So I will declare one function here at the top. This is void, reverse display, reverse display, struct node. So temp is a temporary variable I am using. This is reverse display, 0.5 percent D data. So I will execute this. We will check whether this program is working correctly or not. I will create one linked list. Same linked list is choice 1. Enter data to be inserted is 10. Then choice 1, 20, choice 1, 30. Suppose my singly linked list contains 3 nodes 10, 20 and 30. Now I will display this singly linked list first in forward direction. So it is choice 2. So it is printing 10, 20, 30. This is my original linked list. Suppose choice 3, choice 3 is for displaying the same singly linked list in reverse direction. Now it is printing 30, 20 and 10. So in this problem this is a recursive function I have written. If temp value reaches to null, it is returning. Otherwise I am calling a recursive function, reverse display and passing next nodes address, temp arrow next. So when temp value reaches to null, in that case it is returning to a function from where it is called and then printing. So how it works is initially temp is pointing to a start. Then temp will move forward in forward direction till temp reaches to null. In that case exit condition will be executed and then it is returning to a previous node and then it starts printing from last node to first node. So this is one of the approach for printing a singly linked list in reverse direction. Now in this program we are not actually reversing the singly linked list. We are just printing a singly linked list in reverse direction. Original linked list will remain as it is. So this is a program for displaying singly linked list in reverse direction. We will go to PPT now again. These are the references I have used for creation of this content and PPT and video. Thank you.