C Program To Reverse A Doubly Linked List - Codeforwin
C Program To Reverse A Doubly Linked List - Codeforwin
Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online. A blog for beginners
to advance their skills in programming.
Write a C program to create a doubly linked list and reverse the linked list. How to reverse the doubly linked
list in C programming. Algorithm to reverse a doubly linked list.
Required knowledge
Basic C programming, Functions, Dynamic memory allocation, Doubly linked list
https://codeforwin.org/2015/11/c-program-to-reverse-doubly-linked-list.html 1/5
4/24/2019 C program to reverse a doubly linked list - Codeforwin
current ← temp;
End while
temp ← head;
head ← last;
last ← temp;
End
1. To reverse the list we start with the first node. Say a pointer current keeps track of the current
node. Now initially current points to head node.
3. Move the position of current pointer to its next node. In general, now current.prev holds the
address of next node.
5. When, the current pointer becomes NULL then the list will look something like.
https://codeforwin.org/2015/11/c-program-to-reverse-doubly-linked-list.html 2/5
4/24/2019 C program to reverse a doubly linked list - Codeforwin
6. Finally, swap the head and last pointers and you are done.
7. Now, if you compare the above image to the below given image you will find both are similar linked
list.
29 {
30 int n, data, choice=1;
31
32 head = NULL;
33 last = NULL;
34
35 /*
36 * Runs forever until user chooses 0
37 */
38 while(choice != 0)
39 {
40 printf("============================================\n");
41 printf("DOUBLY LINKED LIST PROGRAM\n");
42 printf("============================================\n");
43 printf("1. Create List\n");
44 printf("2. Reverse List\n");
45 printf("3. Display list\n");
46 printf("0. Exit\n");
47 printf("--------------------------------------------\n");
48 printf("Enter your choice : ");
49
50 scanf("%d", &choice);
51
52 switch(choice)
53 {
54 case 1:
55 printf("Enter the total number of nodes in list: ");
56 scanf("%d", &n);
57 createList(n);
58 break;
59 case 2:
60 reverseList();
61 break;
62 case 3:
63 displayList();
64 break;
65 case 0:
66 break;
67 default:
68 printf("Error! Invalid choice. Please choose between
69 }
70
71 printf("\n\n\n\n\n");
72 }
73
74 return 0;
75 }
76
77
78 /**
79 * Creates a doubly linked list of n nodes.
80 * @n Number of nodes to be created
81 */
( )
Output
https://codeforwin.org/2015/11/c-program-to-reverse-doubly-linked-list.html 4/5
4/24/2019 C program to reverse a doubly linked list - Codeforwin
============================================
DOUBLY LINKED LIST PROGRAM
============================================
1. Create List
2. Reverse List
3. Display list
0. Exit
--------------------------------------------
Enter your choice : 1
Enter the total number of nodes in list: 4
Enter data of 1 node: 10
Enter data of 2 node: 20
Enter data of 3 node: 30
Enter data of 4 node: 40
Happy coding 😉
About Pankaj
Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write
programming articles especially for beginners. He works at Vasudhaika So ware Sols as a So ware
Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and
Music lover.
Follow on: Facebook | Twitter | Google | Website or View all posts by Pankaj
https://codeforwin.org/2015/11/c-program-to-reverse-doubly-linked-list.html 5/5