 Hello everyone, my name is Nikhil Gadjum working as assistant professor in department of computer science and engineering in Palchand Institute of Technology, Solapur. In today's video we will see a program for reversing doubly linked list. So this is learning outcome at the end of this session student will be able to write a C program for reversing the given doubly linked list. So this is doubly linked list. So doubly linked list can be defined as a collection of nodes with each node consists of three values. First one it is a pointer which is points to a previous node. Second one is data and third one is pointer to a next node which is holding address of next node. So this is one of the example of doubly linked list consists of four nodes. Now A, B, C, D are the values or data stored in these four nodes. A is the first node. So first nodes previous pointer will always have null value and last nodes next pointer will always have null value. So indicating that that is a last node and head is a pointer, global pointer which always holds an address of first node. So head is holding address of A in other words we can say that head is pointing to first node A. Now reversing doubly linked list suppose this is an original doubly linked list where 10, 20, 30, 40 and 50 are the five nodes, 10s next is pointing to 20 and 20s previous is pointing to 10 like this. Start it always points to first node so start is pointing to 10, 10s previous will have null value and last nodes 50 next will have null value. So after reversing a doubly linked list, linked list will become like this. Start will point to 50 node and 50s previous will have null value, 50 should become the first node and 10 will become the last node. So 50s next will point to 40 and 40s previous will point to node 50 like this. So in reversing doubly linked list you are not just printing the doubly linked list in reverse order you are actually reversing the doubly linked list. So approach for reversing doubly linked list we will use two pointers called as temp 1 and temp 2. Temp 1 will move in forward direction and temp 2 will move in backward direction. So two way traversing forward and backward traversing is possible in doubly linked list because of two pointers previous and next. Go on swapping the data part of temp 1 and temp 2 till temp 1 and temp 2 will cross over each other. Now how to find out whether temp 1 and temp 2 cross over each other you are not comparing the addresses or value of addresses of temp 1 and temp 2. You have to keep track of temp 1 and temp 2. So how many nodes pointed by temp 1 and how many nodes pointed by temp 2. So by keeping the track of temp 1 and temp 2 you can find out whether temp 1 and temp 2 cross over each other or not. So this is one approach. So this is an example. Suppose this is an original doubly linked list with 10, 20, 30, 40, 50 are the nodes start is pointing to 10. So I am using two pointers temp 1 and temp 2. Temp 1 will point to 10 or first node temp 2 will point to 50 again it is not possible directly to point temp 2 at last node you have to traverse from first node to last node. So temp 2 will point to last node. Then go on swapping the values of temp 1 and temp 2. So after swapping 50 will become now first node and 10 will become now second node. After swapping temp 1 will move in forward direction, temp 2 will move in backward direction. Now temp 1 is pointing to 20, temp 2 is pointing to 40. Again swap these two values then move temp 1 and temp 2 in forward direction and in backward direction. Now temp 1 and temp 2 both are pointing to same node 30. After swapping no effect will be there. So after temp 1 and temp 2 will cross over each other. So whatever linked list will be created that will be your answer. So this is one of the approach of reversing doubly linked list. Now this is an approach too for reversing doubly linked list. Traverse temp from start to last node of doubly linked list. Then go on swapping the previous pointer and next pointer values of each node. Then at last point the start to a last node of original linked list to reverse the actual original doubly linked list. We will see suppose this is my original doubly linked list. So start is pointing to 10 and there are 5 nodes 50 is the last node. So first nodes previous pointer will have null value. Last nodes next pointer will have null value. So I will traverse doubly linked list using temporary pointer. Then I have gone swapping the values of previous pointer and next pointer of each node. So after swapping 2000 will be now stored in previous pointer and null will be stored in the next pointer. Now after these two swapping 3000 will be stored here and 1000 will be stored here. So after swapping 4000 will be stored in previous pointer, 2000 will be stored in next pointer. So here after swapping 5000 will be stored in previous, 3000 will be stored in next pointer and at last null will be stored in previous and 4000 will be stored in next part and start will point to a last node. So, this is actually a reversal of original doubly linked list. So, after reversal 50 will become the first node. This is my first node now. Start will point to first node. Start is pointing to 50. So, 50 is next. We will hold address of 40. So, 50 next, we will hold the address of 40 and 50 is previous will have null value. So, 50 is my first node. So, this is another approach for reversing my original doubly linked list. So, you can pause the video and you can answer is doubly linked list reverse if we point start to the end node from first node only. So, answer is no. By just changing the value of start instead of start will point to first node if we change start will point to last node it is not actually reversing the doubly linked list. So, you have to change the address part or you have to change the data part for reversing actually doubly linked list. So, answer for this question is no. Now, we will see a program or we will write a program for reversing doubly linked list. Suppose this is online JDB site where I can execute a program in different languages. So, I have selected C compiler and I have already a doubly linked list program. I will just check whether it is executing correctly or not. So, I will choose case 1. So, which is insert at beginning function I will insert 50 again case 1 insert 40 again case 1 insert 30 and I will display display is my case 6. So, it is displaying 30 40 50. So, this program is working correctly I will make changes in this program only. So, case 7 is exit instead of case 7 I will make it as case 8 just a second and case 7 will be now my reverse I will write reverse function at case 7 this is slash t. So, I will add case this is my case 7 this is my reverse function case 8 will be exit this will pile loop will execute a end for case 8. I will write now a function called as now reverse this is void reverse. Now, for writing a reverse function I will use first approach. I will use two pointers temp1 and temp2 temp1 will move in forward direction temp2 will move in backward direction. So, this is my struct node star temp1 comma star temp2. Now, temp1 will start from first node and temp2 will start from last node. So, I have to traverse temp2 which will point to last node from I will traverse temp2 from first node to last node. So, this is temp2 equal to start is while temp2 arrow next not equal to null temp2 equal to temp2 arrow next. I will use a counter for counting how many nodes are there in my doubly linked list. So, I will in declare count and initialize to 0. So, after this while loop temp2 will point to a last node and temp1 will point to first node that is start. So, after executing this while loop I have a counter variable which will hold total number of nodes in a doubly linked list. So, I will use now for loop or while loop while I is less than count divided by 2. I will declare I also. So, this while loop is for swapping the data part of temp1 and temp2. So, how many swapping will be done? It will be maximum count divided by 2. So, that is why I have used condition as while I is less than count divided by 2. So, I am swapping the data part of temp1 and temp2 now. So, for swapping I will declare another variable called as temp int t. So, this is t equal to temp1 arrow data temp1 arrow data equal to temp2 arrow data and then temp2 arrow data equal to t. So, this is my swapping. So, after swapping temp1 will move in forward direction. So, this is temp1 equal to temp1 arrow next and temp2 is equal to temp2 arrow previous. So, temp2 will move in backward direction then i plus plus for incrementing i. So, I will execute this we will check whether this code is now working correctly or not. I will create first doubly linked list 50 again 140 130 I will display this. So, this is my original doubly linked list consist of 3 nodes 30 40 and 50. Now, I will call function 7 case 7 for reversing doubly linked list I will display this. Now, doubly linked list now original doubly linked list now reverse. So, data is now 50 40 and 30 where 50 is my starting node now. So, this is a code for now reversing my doubly linked list with approach 1 go again go to ppt now slide. Now, these are some of the references I have used for creation of this video. Thank you.