0% found this document useful (0 votes)
864 views

C Program To Reverse A Doubly Linked List - Codeforwin

This document discusses reversing a doubly linked list in C programming. It provides an algorithm to reverse the list by swapping the next and prev pointers of each node as it traverses the list from head to tail. It also includes a full C program that implements functions to create, reverse, and display a doubly linked list. The program allows the user to choose these options and provides sample output.

Uploaded by

pratik43roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
864 views

C Program To Reverse A Doubly Linked List - Codeforwin

This document discusses reversing a doubly linked list in C programming. It provides an algorithm to reverse the list by swapping the next and prev pointers of each node as it traverses the list from head to tail. It also includes a full C program that implements functions to create, reverse, and display a doubly linked list. The program allows the user to choose these options and provides sample output.

Uploaded by

pratik43roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4/24/2019 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.

C program to reverse a doubly linked list


November 3, 2015 Pankaj Data Structures C, Data Structures, Doubly Linked List, Linked
List, Program

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.

Doubly Linked List

Doubly Linked List Reversed

Required knowledge
Basic C programming, Functions, Dynamic memory allocation, Doubly linked list

Algorithm to reverse a doubly linked list

Algorithm to reverse a doubly linked list


%% Input : head {Pointer to first node of the list}
last {Pointer to last node of the list}
Begin:
current ← head;
While (current != NULL) do
temp ← current.next;
current.next ← current.prev;
current.prev ← temp;

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

Steps to reverse a doubly linked list


There are various methods to reverse a doubly linked list. Here I am using the simplest approach to reverse
the list.

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.

2. Swap the previous and next pointer fields of current node.

3. Move the position of current pointer to its next node. In general, now current.prev holds the
address of next node.

4. Repeat Step 2-3 until current pointer becomes NULL .

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.

Program to reverse a doubly linked list


1 /**
2 * C program to reverse a Doubly linked list
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8
9 /*
10 * Basic structure of Node
11 */
12 struct node {
13 int data;
14 struct node * prev;
15 struct node * next;
16 }*head, *last;
17
18
19
20 /*
21 * Functions used in this program
22 */
23 void createList(int n);
24 void displayList();
25 void reverseList();
26
27
28 int main()
https://codeforwin.org/2015/11/c-program-to-reverse-doubly-linked-list.html 3/5
4/24/2019 C program to reverse a doubly linked list - Codeforwin

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

Have a doubt, write here. I will help my best.


Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>

https://codeforwin.org/2015/11/c-program-to-reverse-doubly-linked-list.html 5/5

You might also like