-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.cpp
223 lines (218 loc) · 4.2 KB
/
DoublyLinkedList.cpp
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
#include<iostream>
using namespace std;
class DoublyLinkedList;
class DNode {
int data;
DNode* next, * prev;
friend class DoublyLinkedList;
};
class DoublyLinkedList {
DNode* head;
public:
DoublyLinkedList() {
head = 0;
}
~DoublyLinkedList() {
while (head != 0)
{
DNode* curr = head;
head = head->next;
delete curr;
}
}
void display() {
if (head == 0)
cout << "The List is empty : \n";
else {
DNode* curr = head;
while (curr != 0) {
cout << curr->data << " ";
curr = curr->next;
}
cout << "\n";
}
}
bool insertAtStart(int val) {
DNode* temp = new DNode;
temp->data = val;
temp->prev = 0;
temp->next = head;
if (head != 0) {
head->prev = temp;
}
head = temp;
return true;
}
bool deleteFromStart() {
if (head == 0)
return 0;
else {
DNode* curr = head;
curr = curr->next;
curr->prev = 0;
delete head;
head = curr;
return true;
}
}
bool insertAtEnd(int val) {
DNode* temp = new DNode;
temp->data = val;
temp->next = 0;
if (head == 0)
{
temp->prev = 0;
head = temp;
}
else {
DNode* curr = head;
DNode* last = 0;
while (curr != 0) {
last = curr;
curr = curr->next;
}
temp->prev = last;
last->next = temp;
return true;
}
}
bool deleteFromEnd() {
if (head == 0)
return 0;
else {
DNode* curr = head;
DNode* last = 0;
while (curr != 0) {
last = curr;
curr = curr->next;
}
if (last->prev == 0) {
delete last;
last = 0;
head = 0;
}
else {
last = last->prev;
DNode* del = last->next;
delete del;
last->next = 0;
del = 0;
return 1;
}
}
}
bool sortedinsert(int val) {
DNode* temp = new DNode;
temp->data = val;
temp->prev = 0;
temp->next = head;
int swapped;
if (head != 0) {
head->prev = temp;
}
head = temp;
if (head->next == 0)
{
return true;
}
else {
do {
swapped = 0;
DNode* curr = head->next;
DNode* last = head;
while (curr != 0) {
if ((last->data) > (curr->data)) {
swap(last->data, curr->data);
swapped = 1;
}
last = curr;
curr = curr->next;
}
} while (swapped);
return true;
}
}
DNode* Search(int val) {
if (head == 0)
return 0;
else {
DNode* curr = head;
while (curr != 0 && curr->data != val) {
curr = curr->next;
}
if (curr == 0)
return 0;
else if (curr->data == val) {
return curr;
}
}
}
bool deleteSpecificValue(int val) {
DNode* del = Search(val);
if (del == 0) {
cout << "The list is empty or the value not found.";
return false;
}
else {
del->next->prev = del->prev;
del->prev->next = del->next;
delete del;
return true;
}
}
bool mergeLists(DoublyLinkedList* Obj2) {
DNode* curr = head;
if (Obj2 == 0)
return false;
else
{
if (head == 0) {
head = Obj2->head;
return true;
}
else
{
while (curr->next != 0) {
curr = curr->next;
}
curr->next = Obj2->head;
Obj2->head->prev = curr;
return true;
}
}
}
};
int main() {
DoublyLinkedList l1, l2;
l1.insertAtStart(3);
l1.insertAtStart(5);
l1.insertAtStart(4);
l2.insertAtStart(12);
l2.insertAtStart(13);
l2.insertAtStart(14);
cout << "THe list after insertion from start : ";
l1.display();
l1.insertAtEnd(10);
l1.insertAtEnd(8);
l1.insertAtEnd(7);
cout << "The list after insertion from end : ";
l1.display();
l1.deleteFromStart();
l1.deleteFromEnd();
cout << "THe list after deletion from start and end : ";
l1.display();
l1.sortedinsert(6);
cout << "The list after sorted insertion : ";
l1.display();
cout << "The address of value present in list : " << l1.Search(5) << "\n";
cout << "The address of value which is not present in list : " << l1.Search(9) << "\n";
l1.deleteSpecificValue(5);
cout << "The list after deleting a specific value : ";
l1.display();
cout << "By deleting a value which is not present in list it shows : ";
l1.deleteSpecificValue(9);
l1.mergeLists(&l2);
l1.display();
system("pause>nul");
return 0;
}