C++ Program To Implement Doubly Linked List - Sanfoundry
C++ Program To Implement Doubly Linked List - Sanfoundry
Here is source code of the C++ Program to demonstrate doubly single linked list. The C++
program is successfully compiled and run on a Linux system. The program output is also shown
below.
1. /*
2. * C++ Program to Implement Doubly Linked List
3. */
4. #include<iostream>
5. #include<cstdio>
6. #include<cstdlib>
7. /*
8. * Node Declaration
9. */
10. using namespace std;
11. struct node
12. {
13. int info;
14. struct node *next;
15. struct node *prev;
16. }*start;
code.whitehatjr.com
17.
OPEN
Kids Coding Online Classes
18. /*
19. Class Declaration
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 1/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
20. */
21. class double_llist
22. {
23. public:
24. void create_list(int value);
25. void add_begin(int value);
26. void add_after(int value, int position);
27. void delete_element(int value);
28. void search_element(int value);
29. void display_dlist();
30. void count();
31. void reverse();
32. double_llist()
33. {
34. start = NULL;
35. }
36. };
37.
38. /*
39. * Main: Conatins Menu
40. */
41. int main()
42. {
43. int choice, element, position;
44. double_llist dl;
45. while (1)
46. {
47. cout<<endl<<"----------------------------"<<endl;
48. cout<<endl<<"Operations on Doubly linked list"<<endl;
49. cout<<endl<<"----------------------------"<<endl;
50. cout<<"1.Create Node"<<endl;
51. cout<<"2.Add at begining"<<endl;
52. cout<<"3.Add after position"<<endl;
53. cout<<"4.Delete"<<endl;
54. cout<<"5.Display"<<endl;
55. cout<<"6.Count"<<endl;
56. cout<<"7.Reverse"<<endl;
57. cout<<"8.Quit"<<endl;
58. cout<<"Enter your choice : ";
59. cin>>choice;
60. switch ( choice )
61. {
62. case 1:
63. cout<<"Enter the element: ";
64. cin>>element;
65. dl.create_list(element);
66. cout<<endl;
67. break;
68. case 2:
69. cout<<"Enter the element: ";
70. cin>>element;
71. dl.add_begin(element);
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 2/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
72. cout<<endl;
73. break;
74. case 3:
75. cout<<"Enter the element: ";
76. cin>>element;
77. cout<<"Insert Element after postion: ";
78. cin>>position;
79. dl.add_after(element, position);
80. cout<<endl;
81. break;
82. case 4:
83. if (start == NULL)
84. {
85. cout<<"List empty,nothing to delete"<<endl;
86. break;
87. }
88. cout<<"Enter the element for deletion: ";
89. cin>>element;
90. dl.delete_element(element);
91. cout<<endl;
92. break;
93. case 5:
94. dl.display_dlist();
95. cout<<endl;
96. break;
97. case 6:
98. dl.count();
99. break;
100. case 7:
101. if (start == NULL)
102. {
103. cout<<"List empty,nothing to reverse"<<endl;
104. break;
105. }
106. dl.reverse();
107. cout<<endl;
108. break;
109. case 8:
110. exit(1);
111. default:
112. cout<<"Wrong choice"<<endl;
113. }
114. }
115. return 0;
116. }
117.
118. /*
119. * Create Double Link List
120. */
121. void double_llist::create_list(int value)
122. {
123. struct node *s, *temp;
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 3/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 4/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
176. {
177. q = q->next;
178. if (q == NULL)
179. {
180. cout<<"There are less than ";
181. cout<<pos<<" elements."<<endl;
182. return;
183. }
184. }
185. tmp = new(struct node);
186. tmp->info = value;
187. if (q->next == NULL)
188. {
189. q->next = tmp;
190. tmp->next = NULL;
191. tmp->prev = q;
192. }
193. else
194. {
195. tmp->next = q->next;
196. tmp->next->prev = tmp;
197. q->next = tmp;
198. tmp->prev = q;
199. }
200. cout<<"Element Inserted"<<endl;
201. }
202.
203. /*
204. * Deletion of element from the list
205. */
206. void double_llist::delete_element(int value)
207. {
208. struct node *tmp, *q;
209. /*first element deletion*/
210. if (start->info == value)
211. {
212. tmp = start;
213. start = start->next;
214. start->prev = NULL;
215. cout<<"Element Deleted"<<endl;
216. free(tmp);
217. return;
218. }
219. q = start;
220. while (q->next->next != NULL)
221. {
222. /*Element deleted in between*/
223. if (q->next->info == value)
224. {
225. tmp = q->next;
226. q->next = tmp->next;
227. tmp->next->prev = q;
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 5/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 6/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
280. }
281.
282. /*
283. * Reverse Doubly Link List
284. */
285. void double_llist::reverse()
286. {
287. struct node *p1, *p2;
288. p1 = start;
289. p2 = p1->next;
290. p1->next = NULL;
291. p1->prev = p2;
292. while (p2 != NULL)
293. {
294. p2->prev = p2->next;
295. p2->next = p1;
296. p1 = p2;
297. p2 = p2->prev;
298. }
299. start = p1;
300. cout<<"List Reversed"<<endl;
301. }
advertisement
$ g++ doubly_llist.cpp
$ a.out
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 7/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 2
Enter the element: 100
First Create the list.
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 3
Enter the element: 200
Insert Element after postion: 1
First Create the list.
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 4
List empty,nothing to delete
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 8/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
List empty,nothing to display
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 6
Number of elements are: 0
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 7
List empty,nothing to reverse
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 9/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 1
Enter the element: 100
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
100 <-> NULL
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 2
Enter the element: 200
Element Inserted
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 10/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
200 <-> 100 <-> NULL
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 3
Enter the element: 50
Insert Element after postion: 2
Element Inserted
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
200 <-> 100 <-> 50 <-> NULL
---------------------------------
Operations on Doubly linked list
---------------------------------
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 11/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 3
Enter the element: 150
Insert Element after postion: 3
Element Inserted
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
200 <-> 100 <-> 50 <-> 150 <-> NULL
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 6
Number of elements are: 4
---------------------------------
Operations on Doubly linked list
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 12/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 4
Enter the element for deletion: 50
Element Deleted
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
200 <-> 100 <-> 150 <-> NULL
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 6
Number of elements are: 3
---------------------------------
Operations on Doubly linked list
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 13/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 7
List Reversed
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
150 <-> 100 <-> 200 <-> NULL
---------------------------------
Operations on Doubly linked list
---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 3
Enter the element: 200
Insert Element after postion: 100
There are less than 100 elements.
---------------------------------
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 14/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 15/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
advertisement
Participate in the Sanfoundry Certi cation contest to get free Certi cate of Merit. Join our social
networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 16/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
Recommended Posts:
1. C Programming Examples on File Handling
2. C++ Programming Examples on Combinatorial Problems & Algorithms
3. Java Programming Examples on Arrays
4. C# Programming Examples on Strings
5. Java Algorithms, Problems & Programming Examples
6. Python Programming Examples on Trees
7. C Programming Examples on Stacks & Queues
8. C Programming Examples on Strings
9. Data Structure Questions and Answers
10. C Programming Examples
11. C Programming Examples on Bitwise Operations
12. C# Programming Examples on Data Structures
13. C Programming Examples on Trees
14. C Programming Examples using Recursion
15. Java Programming Examples on Data-Structures
16. C Programming Examples on Data-Structures
17. C Programming Examples without using Recursion
18. C++ Programming Examples on Data-Structures
19. Python Programming Examples on Linked Lists
20. C Programming Examples on Linked List
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is
Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN
Architect and is passionate about competency developments in these areas.
He lives in Bangalore and delivers focused training sessions to IT
professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux
Networking, Linux Storage, Advanced C Programming, SAN Storage
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 17/18
22/09/2020 C++ Program To Implement Doubly Linked List - Sanfoundry
Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with
him @ LinkedIn
Name*
Email*
Subscribe
About | Certi cations | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact
https://www.sanfoundry.com/cpp-program-implement-doubly-linked-list/ 18/18