-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly_linked_list.h
271 lines (247 loc) · 6.04 KB
/
doubly_linked_list.h
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
#ifndef DOUBLY_LINKED_LIST_H
#define DOUBLY_LINKED_LIST_H
#include <iostream>
using namespace std;
template <class T>
class doubly_linked_list
{
struct Node
{
T value;
Node* previous;
Node* next;
};
Node* head;
Node* tail;
int length;
public:
doubly_linked_list()
{
length = 0;
head = tail = nullptr;
}
T front()
{
return head->value;
}
T back()
{
return tail->value;
}
void insert_back(T item)
{
Node* newItem = new Node;
newItem->value = item;
newItem->next = nullptr;
if (!length)
{
newItem->previous = nullptr;
head = newItem;
}
else
{
tail->next = newItem;
newItem->previous = tail;
}
tail = newItem;
length++;
}
void insert_front(T item)
{
Node* newItem = new Node;
newItem->value = item;
newItem->previous = nullptr;
if (!length)
{
newItem->next = nullptr;
tail = newItem;
}
else
{
newItem->next = head;
}
head = newItem;
length++;
}
void insert_in_pos(T item, int pos)
{
if (pos == 0) insert_front(item);
else if (pos == length) insert_back(item);
while (pos < 0 || pos > length)
{
cout << "Please insert at index within [0 - " << length - 1 << "]: ";
cin >> pos;
}
Node* newItem = new Node;
newItem->value = item;
Node* current = head;
for (int i = 0; i < pos - 1; i++)
{
current = current->next;
}
newItem->next = current->next;
newItem->previous = current;
current->next = newItem;
current->next->previous = newItem;
length++;
}
void remove_front()
{
if (length > 1)
{
const Node* current = head;
head = head->next;
delete current;
length--;
}
else if (length == 1) head = nullptr, length--;
}
void remove_back()
{
if (length > 1)
{
const Node* current = tail;
tail = tail->previous;
delete current;
length--;
}
else if (length == 1) tail = nullptr, length--;
}
int count(T element)
{
int counter = 0;
Node* current = head;
for (int i = 0; i < length; i++)
{
if (current->value == element) counter++;
current = current->next;
}
return counter;
}
void remove_from_pos(const int pos)
{
if (pos == 1) remove_front();
else if (pos == length) remove_back();
else if (pos < 1 || pos > length) cout << "Position is out of range\n";
else
{
Node* current = head;
Node* temp = head;
Node* temp2 = head;
for (int i = 1; i < pos - 1; i++)
temp = temp->next;
for (int i = -1; i < pos - 1; i++)
temp2 = temp2->next;
for (int i = 0; i < pos - 1; i++)
current = current->next;
temp->next = current->next;
temp2->previous = current->previous;
delete current;
length--;
}
}
bool is_empty() const
{
return !length;
}
int is_exist(T element)
{
Node* current = head;
for (int i = 0; i < length; i++)
{
if (current->value == element)
{
return i;
}
current = current->next;
}
return -1;
}
pair<T, int> max_element()
{
int max = INT_MIN, index = 0;
Node* current = head;
for (int i = 0; i < length; i++)
{
if (current->value > max) max = current->value, index = i;
current = current->next;
}
return make_pair(max, index);
}
pair<T, int> min_element()
{
int min = INT_MAX, index = 0;
Node* current = head;
for (int i = 0; i < length; i++)
{
if (current->value < min) min = current->value, index = i;
current = current->next;
}
return make_pair(min, index);
}
void modify_at_pos(const int pos, T item)
{
Node* current = head;
for (int i = 0; i < pos; i++)
{
current = current->next;
}
current->value = item;
}
void clear()
{
head = tail = nullptr, length = 0;
}
int size() const
{
return length;
}
friend ostream& operator<<(ostream& print, doubly_linked_list<T>& list)
{
if (!list.size())
{
print << "The list is empty.\n";
}
else
{
Node* current = list.head;
int temp = list.size() - 1;
print << "List: {";
print << current->value;
current = current->next;
while (temp--)
{
print << ", " << current->value;
current = current->next;
}
print << "}";
}
return print;
}
T& operator[](const int pos)
{
Node* current = head;
try
{
if (pos < 0 || pos >= length)
{
throw "Error! Index is out of range.";
}
for (int i = 0; i < pos; i++)
{
current = current->next;
}
}
catch (const char* e)
{
cout << e;
}
return current->value;
}
~doubly_linked_list()
{
delete head;
delete tail;
}
};
#endif