-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_linked_list.cpp
More file actions
229 lines (204 loc) · 4.26 KB
/
double_linked_list.cpp
File metadata and controls
229 lines (204 loc) · 4.26 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
#include <iostream>
#include "double_linked_list.h"
bool isEmpty(NPTR &list)
{
if(list == nullptr)
return true;
return false;
}
NPTR createNode(const int &value)
{
NPTR plist = new NODE;
plist->info = value;
plist->next = nullptr;
plist->prev = nullptr;
return plist;
}
void insertFront(NPTR &list, const int &value, NPTR &lastNode)
{
NPTR newNode = createNode(value);
if(isEmpty(list))
{
list = newNode;
lastNode = newNode;
return;
}
newNode->next = list;
list = newNode;
list->prev = nullptr;
}
void insertEnd(NPTR &list, const int &value, NPTR &lastNode)
{
NPTR newNode = createNode(value);
if(isEmpty(list))
{
lastNode = newNode;
list = newNode;
return;
}
newNode->prev = lastNode;
lastNode->next = newNode;
lastNode = newNode;
}
int countNode(NPTR list)
{
NPTR prevNode = nullptr;
int count = 1;
while(list != nullptr)
{
list->prev = prevNode;
prevNode = list;
list = list->next;
count++;
}
return count - 1;
}
void delFront(NPTR &list)
{
NPTR delNode = nullptr;
if(isEmpty(list))
{
std::cout << "Node cannot be deleted. List is empty" << std::endl;
return;
}
if(countNode(list) == 1)
{
delete list;
list = nullptr;
return;
}
delNode = list;
list = list->next;
list->prev = nullptr;
delete delNode;
}
void delEnd(NPTR &list, NPTR &lastNode)
{
NPTR prevNode = nullptr;
NPTR delNode = nullptr;
NPTR bak = list;
if(isEmpty(list))
{
std::cout << "Element cannot be deleted. Empty list" << std::endl;
return;
}
if(countNode(list) == 1)
{
delete list;
list = nullptr;
return;
}
while(list->next != lastNode)
{
list->prev = prevNode;
prevNode = list;
list = list->next;
}
list->prev = prevNode;
list->next = nullptr;
delNode = lastNode;
lastNode = list;
delete delNode;
delNode = nullptr;
list = bak;
}
void delList(NPTR &list)
{
NPTR delNode = nullptr;
NPTR prevNode = nullptr;
if(isEmpty(list))
{
std::cout << "List is already empty!" << std::endl;
return;
}
while(list != nullptr)
{
delNode = list;
list->prev = prevNode;
prevNode = list;
list = list->next;
delete delNode;
}
}
void display(NPTR list)
{
NPTR prevNode = nullptr;
if(isEmpty(list))
{
std::cout << "List is Empty!" << std::endl;
return;
}
std::cout << "The list is -" << std::endl;
while(list != nullptr)
{
std::cout << list->info << " ";
list->prev = prevNode;
prevNode = list;
list = list->next;
}
std::cout << "\n";
}
void revDisplay(NPTR list, NPTR &lastNode)
{
NPTR nextNode = nullptr;
if(isEmpty(list))
{
std::cout << "List is Empty!" << std::endl;
return;
}
while(lastNode != nullptr)
{
std::cout << lastNode->info << " ";
/*lastNode->next = nextNode;
nextNode = lastNode;*/
lastNode = lastNode->prev;
}
lastNode = list;
std::cout << "\n";
}
int main()
{
NPTR list = nullptr;
NPTR lastNode = nullptr;
int value, ch, nodes = 0;
do
{
std::cout << "------------M E N U------------" << std::endl;
std::cout << "1. Insert a node at the beginning of the list" << std::endl;
std::cout << "2. Insert a node at the end of the list" << std::endl;
std::cout << "3. Count the number of nodes" << std::endl;
std::cout << "4. Delete from front of the list" << std::endl;
std::cout << "5. Delete at the end of the list" << std::endl;
std::cout << "6. Display the list" << std::endl;
std::cout << "7. Display the list in reverse order" << std::endl;
std::cout << "8. Exit" << std::endl;
std::cout << "Enter your choice" << std::endl;
std::cin >> ch;
switch(ch)
{
case 1 : std::cout << "Please enter a value" << std::endl;
std::cin >> value;
insertFront(list, value, lastNode);
break;
case 2 : std::cout << "Please enter a value" << std::endl;
std::cin >> value;
insertEnd(list, value, lastNode);
break;
case 3 : nodes = countNode(list);
std::cout << "The number of nodes are" << " " << nodes << std::endl;
break;
case 4 : delFront(list);
break;
case 5 : delEnd(list, lastNode);
break;
case 6 : display(list);
break;
case 7 : revDisplay(list, lastNode);
break;
case 8 : delList(list);
break;
default : std::cout << "Invalid Choice" << std::endl;
}
}while(ch != 8);
std::cout << "THANK YOU !" << std::endl;
}