Skip to content

Commit 6bf015d

Browse files
Doubly linked list in C++
1 parent 8023f23 commit 6bf015d

File tree

4 files changed

+470
-0
lines changed

4 files changed

+470
-0
lines changed
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
7+
/*Doubly linked list*/
8+
9+
#include<iostream>
10+
#include "Node.h"
11+
#include "Doubly.h"
12+
13+
using namespace std;
14+
15+
DoublyLinkedList::DoublyLinkedList()
16+
{
17+
start = NULL;
18+
}
19+
20+
DoublyLinkedList::DoublyLinkedList(const DoublyLinkedList& L)
21+
{
22+
if( L.start == NULL )
23+
{
24+
start = NULL;
25+
return;
26+
}
27+
Node *p1,*p2, *previous;
28+
29+
p1 = L.start;
30+
p2 = start = new Node(p1->info);
31+
previous = NULL;
32+
33+
p1 = p1->next;
34+
35+
while( p1 != NULL )
36+
{
37+
p2->next = new Node(p1->info);
38+
p2->prev = previous;
39+
previous = p2;
40+
p2 = p2->next;
41+
p1 = p1->next;
42+
}
43+
p2->prev = previous;
44+
}
45+
46+
DoublyLinkedList::~DoublyLinkedList()
47+
{
48+
Node *p;
49+
while( start != NULL )
50+
{
51+
p = start->next;
52+
delete start;
53+
start = p;
54+
}
55+
}
56+
57+
inline bool DoublyLinkedList::isEmpty() const
58+
{
59+
return start == NULL;
60+
}
61+
62+
void DoublyLinkedList::displayList() const
63+
{
64+
Node *p;
65+
if(isEmpty())
66+
{
67+
cout<<"List is empty\n";
68+
return;
69+
}
70+
p = start;
71+
cout<<"List is :\n";
72+
while( p != NULL )
73+
{
74+
cout << p->info << " ";
75+
p = p->next;
76+
}
77+
cout<<"\n";
78+
}
79+
80+
inline void DoublyLinkedList::insertInEmptyList(int data)
81+
{
82+
Node *temp = new Node(data);
83+
start = temp;
84+
}
85+
86+
void DoublyLinkedList::insertAtBeginning(int data)
87+
{
88+
if(isEmpty())
89+
{
90+
insertInEmptyList(data);
91+
return;
92+
}
93+
Node *temp = new Node(data);
94+
temp->next = start;
95+
start->prev = temp;
96+
start = temp;
97+
}
98+
99+
void DoublyLinkedList::insertAtEnd(int data)
100+
{
101+
if(isEmpty())
102+
{
103+
insertInEmptyList(data);
104+
return;
105+
}
106+
107+
Node *temp = new Node(data);
108+
109+
Node *p = start;
110+
while( p->next != NULL )
111+
p = p->next;
112+
113+
p->next = temp;
114+
temp->prev = p;
115+
}
116+
117+
void DoublyLinkedList::createList()
118+
{
119+
int i,n,data;
120+
121+
cout << "Enter the number of nodes : ";
122+
cin >> n;
123+
124+
if( n <= 0 )
125+
return;
126+
127+
cout << "Enter the first element to be inserted : ";
128+
cin >> data;
129+
insertInEmptyList(data);
130+
131+
Node *p = start;
132+
for( i=2; i<=n; i++ )
133+
{
134+
cout << "Enter the next element to be inserted : ";
135+
cin >> data;
136+
Node *temp = new Node(data);
137+
138+
p->next = temp;
139+
temp->prev = p;
140+
p = temp;
141+
}
142+
}
143+
144+
void DoublyLinkedList::insertAfter(int data, int x)
145+
{
146+
Node *temp = new Node(data);
147+
148+
Node *p = start;
149+
while( p != NULL )
150+
{
151+
if( p->info == x )
152+
break;
153+
p = p->next;
154+
}
155+
if(p == NULL)
156+
cout << x << " not present in the list\n\n";
157+
else
158+
{
159+
temp->prev = p;
160+
temp->next = p->next;
161+
if( p->next != NULL )
162+
p->next->prev = temp; //should not be done when p points to last node
163+
p->next = temp;
164+
}
165+
}
166+
167+
void DoublyLinkedList::insertBefore(int data,int x)
168+
{
169+
Node *temp;
170+
if( isEmpty() )
171+
{
172+
cout<<"List is empty\n";
173+
return;
174+
}
175+
if( start->info == x ) // x is in first node
176+
{
177+
insertAtBeginning(data);
178+
return;
179+
}
180+
Node *p = start;
181+
while( p != NULL )
182+
{
183+
if( p->info == x )
184+
break;
185+
p = p->next;
186+
}
187+
188+
if ( p == NULL )
189+
cout << x << "not present in the list\n";
190+
else
191+
{
192+
temp = new Node(data);
193+
temp->prev = p->prev;
194+
temp->next = p;
195+
p->prev->next = temp;
196+
p->prev = temp;
197+
}
198+
}
199+
200+
void DoublyLinkedList::deleteFirstNode()
201+
{
202+
if( isEmpty() )
203+
{
204+
cout<<"List is empty\n";
205+
return;
206+
}
207+
208+
Node *temp = start;
209+
if (start->next == NULL) // list has only one node
210+
start = NULL;
211+
else
212+
{
213+
start = start->next;
214+
start->prev = NULL;
215+
}
216+
delete temp;
217+
}
218+
219+
void DoublyLinkedList::deleteLastNode()
220+
{
221+
if( isEmpty() )
222+
{
223+
cout<<"List is empty\n";
224+
return;
225+
}
226+
227+
Node *temp;
228+
229+
if( start->next == NULL ) // list has only one node
230+
{
231+
temp = start;
232+
start = NULL;
233+
}
234+
else
235+
{
236+
temp = start;
237+
while( temp->next != NULL )
238+
temp = temp->next;
239+
temp->prev->next = NULL;
240+
}
241+
delete temp;
242+
}
243+
244+
void DoublyLinkedList::deleteNode(int x)
245+
{
246+
if(isEmpty())
247+
{
248+
cout<<"List is empty\n";
249+
return;
250+
}
251+
252+
Node *temp;
253+
if( start->next == NULL ) /*only one node in the list*/
254+
if( start->info == x )
255+
{
256+
temp = start;
257+
start = NULL;
258+
delete temp;
259+
return;
260+
}
261+
else
262+
{
263+
cout<<"Element "<< x <<" not found\n";
264+
return;
265+
}
266+
/*Deletion of first node*/
267+
if( start->info == x )
268+
{
269+
temp = start;
270+
start = start->next;
271+
start->prev = NULL;
272+
delete temp;
273+
return;
274+
}
275+
276+
temp = start->next;
277+
while( temp->next != NULL )
278+
{
279+
if( temp->info == x )
280+
break;
281+
temp = temp->next;
282+
}
283+
284+
if( temp->next != NULL )
285+
{
286+
temp->prev->next = temp->next;
287+
temp->next->prev = temp->prev;
288+
delete temp;
289+
}
290+
else
291+
{
292+
if( temp->info == x ) //node to be deleted is last node
293+
{
294+
temp->prev->next = NULL;
295+
delete temp;
296+
}
297+
else
298+
cout << x << "not found\n";
299+
}
300+
}
301+
302+
303+
void DoublyLinkedList::reverseList()
304+
{
305+
if( isEmpty() )
306+
{
307+
cout<<"List is empty\n";
308+
return;
309+
}
310+
311+
Node *p = start;
312+
Node *temp;
313+
while( p->next != NULL )
314+
{
315+
temp = p->next;
316+
p->next = p->prev;
317+
p->prev = temp;
318+
p = p->prev;
319+
}
320+
p->next = p->prev;
321+
p->prev = NULL;
322+
start = p;
323+
}
324+
325+
326+
327+

0 commit comments

Comments
 (0)