Repository navigation
Expand file tree
/
Copy pathDoublyLinkedList.cpp
More file actions
279 lines (239 loc) · 5.12 KB
/
Copy pathDoublyLinkedList.cpp
File metadata and controls
279 lines (239 loc) · 5.12 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
//
// Created by Dr. Ahmed Said on 07/03/2025.
//
#include <iostream>
typedef int ElementType;
class Node {
public:
Node() {
next = nullptr;
prev = nullptr;
};
ElementType data;
Node* next;
Node* prev;
};
typedef Node* Position;
class DoublyList {
private:
Position head; //points to the first node
Position tail; //points to the last node
int counter;
public:
DoublyList() {
head = nullptr;
tail = nullptr;
counter = 0;
}
void MakeNull();
Position First();
Position End();
Position Next(Position p);
Position Previous(Position p);
void InsertAtEnd(ElementType x);
void InsertAtStart(ElementType x);
void InsertAt(ElementType x, Position p);
void InsertAfter(ElementType x, Position p);
void InsertBefore(ElementType x, Position p);
void Delete(Position p);
Position Locate(int x);
ElementType Retrieve(Position p);
void PrintList();
};
void DoublyList::MakeNull() {
head = nullptr;
tail = nullptr;
counter = 0;
// need to add codes to free (delete) nodes memory allocation
}
Position DoublyList::First() {
return head;
}
Position DoublyList::Next(Position p) {
if(p == nullptr) {
return nullptr;
}
return p->next;
}
Position DoublyList::Previous(Position p) {
if(p == nullptr) {
return nullptr;
}
return p->prev;
}
Position DoublyList::End() {
return tail;
}
void DoublyList::InsertAtStart(ElementType x) {
Position newNode = new Node();
newNode->data = x;
newNode->next = head;
newNode->prev = nullptr;
if(head != nullptr) {
head->prev = newNode;
}
head = newNode;
if(tail == nullptr) {
tail = head;
}
counter++;
}
void DoublyList::InsertAtEnd(ElementType x) {
Position newNode = new Node();
newNode->data = x;
newNode->next = nullptr;
newNode->prev = tail;
if(tail != nullptr) {
tail->next = newNode;
}
tail = newNode;
if(head == nullptr) {
head = tail;
}
counter++;
}
// This method insert node after p
void DoublyList::InsertAt(ElementType x, Position p = nullptr) {
if(p == nullptr) {
InsertAtEnd(x);
}
else if(p == head) {
InsertAtStart(x);
}
else {
Position newNode = new Node();
newNode->data = x;
newNode->prev = p->prev;
newNode->next = p;
if(p->prev != nullptr) {
p->prev->next = newNode;
}
p->prev = newNode;
counter++;
}
}
void DoublyList::InsertAfter(ElementType x, Position p) {
if(p == nullptr) {
InsertAtEnd(x);
}
else if(p == head) {
InsertAtStart(x);
}
else {
Position newNode = new Node();
newNode->data = x;
newNode->next = p->next;
newNode->prev = p;
if(p->next != nullptr) {
p->next->prev = newNode;
}
p->next = newNode;
if(p == tail) {
tail = newNode;
}
counter++;
}
}
void DoublyList::InsertBefore(ElementType x, Position p) {
InsertAt(x, p);
}
void DoublyList::Delete(Position p) {
if(p == nullptr) {
// No Element to Delete
return;
}
if(p == tail) {
tail = p->prev;
}
if(p == head) {
head = p->next;
}
Position temp = p;
if(p->prev != nullptr) {
p->prev->next = p->next;
}
if(p->next != nullptr) {
p->next->prev = p->prev;
}
delete temp;
counter--;
}
Position DoublyList::Locate(ElementType x) {
Position p = head;
while(p != nullptr) {
if(p->data == x)
return p;
p = p->next;
}
return nullptr;
}
ElementType DoublyList::Retrieve(Position p) {
if(p == nullptr) {
return NULL;
}
return p->data;
}
void DoublyList::PrintList() {
Position p = head;
while(p != nullptr) {
std::cout << p->data << " -> ";
p = p->next;
}
}
int main() {
DoublyList list;
list.InsertAtStart(1);
list.InsertAtStart(2);
list.InsertAtStart(3);
list.InsertAtStart(4);
list.InsertAt(10, list.End());
list.InsertAt(-30, list.First());
list.InsertAt(-10, list.First());
list.InsertAt(20, list.End());
Position n10 = list.Locate(10);
list.Delete(n10);
Position n20 = list.Locate(20);
list.Delete(n20);
list.PrintList();
return 0;
}
//calculates the summation of all values in the list.
ElementType Sum(DoublyList L)
{
Position curr=L.First();
ElementType s=0;
while(curr!=NULL)
{
ElementType x=L.Retrieve(curr);
s=s+x;
curr=L.Next(curr);
}
return s;
}
DoublyList Reverse(DoublyList l1)
{
DoublyList l2;
Position pos=l1.First();
while (pos!=NULL)
{
int x=l1.Retrieve(pos);
l2.InsertAt(x,l2.First());
pos=l1.Next(pos);
}
return l2;
}
//splits the linked to two lists, one for odd numbers and one for even numbers
void Split(DoublyList L, DoublyList& lOdd, DoublyList& lEven)
{
Position curr=L.First();
//while(curr!=L.END())
while(curr!=NULL)
{
int x=L.Retrieve(curr);
if (x%2==0)
lEven.InsertAt(x);
else
lOdd.InsertAt(x);
curr=L.Next(curr);
}
}