-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLinkedList.cpp
More file actions
304 lines (239 loc) · 5.21 KB
/
Copy pathDLinkedList.cpp
File metadata and controls
304 lines (239 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Name : Annie Bhalla
Roll No : 19HCS4009
Course : Bsc (H) Computer Science
Semester : 3
Subject : Data Structures
Title : Implementation of Doubly Linked List
*/
#include<iostream>
using namespace std;
typedef int Elem;
class Dnode
{
public:
Dnode *next;
Dnode *prev;
Elem elem;
friend class DLinkedList;
};
class DLinkedList
{
private:
Dnode* header;
Dnode* trailer;
// protected:
// void add(Dnode* v,const Elem& e);
// void remove(Dnode* v);
public:
DLinkedList(); //1
~DLinkedList(); //2
bool empty(); //3
Elem& front() const; //4
Elem& back() const; //5
void addFront(const Elem& e); //6
void addBack(const Elem& e); //7
void delFront(); //8
void delBack(); //9
Dnode* middle() const ; //10
friend ostream& operator<<(ostream& ,const DLinkedList& ); //11
void display(); //12
Dnode* search(const Elem& e); //13
void concatenate(DLinkedList& L); //14
};
// (1) Constructor
DLinkedList::DLinkedList()
{
header= new Dnode;
trailer= new Dnode;
header->next=trailer;
trailer->prev= header;
}
// (2) Destructor
DLinkedList::~DLinkedList()
{
while( !empty()) delFront();
delete header;
delete trailer;
}
// (3) To check whether linked list is empty
bool DLinkedList::empty()
{
if(header->next==trailer)
return 1;
else
return 0;
}
// (4) to return the element at head
Elem& DLinkedList::front() const
{
if(header->next==trailer ) throw " EMPTY LIST ";
return (header->next->elem);
}
// (5) to return the element at the last
Elem& DLinkedList::back() const
{
if( header->next==trailer) throw " EMPTY LIST ";
return (trailer->prev->elem);
}
// (6) to add an element in the front
void DLinkedList::addFront(const Elem& e)
{
Dnode *p= new Dnode;
p->elem=e;
p->prev= header;
p->next= header->next;
header->next->prev= p;
header->next=p;
}
// (7) to add an element at the back
void DLinkedList::addBack(const Elem& e)
{
Dnode *p = new Dnode;
p->elem= e;
p->next = trailer;
p->prev= trailer->prev;
trailer->prev->next =p;
trailer->prev=p;
}
// (8) to remove from front
void DLinkedList::delFront()
{
if( empty() ) throw " Empty list ";
Dnode *p= header->next;
header->next = p->next;
p->next->prev = p->prev;
delete p;
}
// (9) to remove from back
void DLinkedList::delBack()
{
if( empty() )
throw " Empty list ";
Dnode *p= trailer->prev;
trailer->prev = p->prev;
p->prev->next = p->next;
delete p;
}
// (10) to return the middle element
Dnode* DLinkedList:: middle()const
{
if(header->next==trailer) throw " Empty List ";
Dnode *q= header->next;
Dnode *x= trailer->prev;
while( q!=x && q->next!=x)
{
q=q->next;
x=x->prev;
}
return q;
}
// (11) overloading output operator
ostream& operator<<(ostream& out, const DLinkedList& D)
{
if(D.header->next==D.trailer)
out<<" ";
else
{
Dnode *p = D.header->next;
while( p->next!=D.trailer)
{
out<<p->elem<<" ";
}
}
return out;
}
// (12) display function
void DLinkedList::display()
{
if(empty())
throw " empty list ";
{
Dnode *n=header->next;
int a;
while(n!=trailer){
a=n->elem;
cout<<a<<" ";
n=n->next;
}cout<<endl;
}
}
// (13) to search for an elemenet e and return its pointer
Dnode* DLinkedList::search(const Elem& e)
{
Dnode* p= header->next;
while(p!=trailer)
{
if(p->elem == e)
return p;
p=p->next;
}
return NULL;
}
// (14) To concatenate two doubly linked lists
void DLinkedList::concatenate(DLinkedList& L)
{
if( empty() && L.empty())
{
cout<<"\n OOPS ! Both the lists are empty ";
return;
}
if(empty())
{
L.display();
return;
}
if( L.empty())
{
display();
return;
}
Dnode* first= trailer->prev;
Dnode* second= L.header->next;
first->next= second;
second->prev= first;
trailer = L.trailer;
L.header->next = trailer;
display();
L.display();
}
// driver code
int main()
{
try
{
DLinkedList l1,l2,l3,l4;
l4.addBack(111);
l4.addFront(98);
l4.addBack(99);
l4.addBack(100);
l4.display();
l1.addFront(7);
l1.addFront(5);
l1.addBack(9);
l1.addBack(10);
l1.addFront(3);
l1.display();
l1.delBack();
l1.display();
l1.delFront();
l1.display();
cout<<"\n Middle element at: "<<l1.middle();
Dnode *q = l1.search(10);
cout<<"\n Pointer of searched elemnet :";
if(q==NULL)
cout<<"\n Element does not exist ";
else
cout<<q;
cout<<endl<<endl;
l1.concatenate(l2);
l2.concatenate(l1);
l2.concatenate(l3); // both are empty
cout<<endl;
l1.concatenate(l4);
}
catch(const char* str)
{
cout<<endl<<" Exception Occured : "<<str;
}
return 0;
}