-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.h
282 lines (246 loc) · 6.7 KB
/
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
272
273
274
275
276
277
278
279
280
281
282
// Copyright Teki
#ifndef STRUCTURES_LINKED_LIST_H
#define STRUCTURES_LINKED_LIST_H
#include <cstdint> // std::size_t
#include <stdexcept> // C++ exceptions
template<typename T>
class LinkedList {
public:
LinkedList();
~LinkedList();
void clear();
void push_back(const T& data);
void push_front(const T& data);
void insert(const T& data, std::size_t index);
void insert_sorted(const T& data);
T pop(std::size_t index);
T pop_back();
T pop_front();
void remove(const T& data);
bool empty() const;
bool contains(const T& data) const;
std::size_t find(const T& data) const;
std::size_t size() const;
T& at(std::size_t index);
T& at(std::size_t index) const;
private:
class Node { // Elemento
public:
explicit Node(const T& data):
data_{data}
{}
Node(const T& data, Node* next):
data_{data},
next_{next}
{}
T& data() {
return data_;
}
const T& data() const {
return data_;
}
Node* next() {
return next_;
}
const Node* next() const {
return next_;
}
void next(Node* node) {
next_ = node;
}
private:
T data_;
Node* next_{nullptr};
};
Node* end() {
auto it = head;
for (auto i = 1u; i < size(); ++i) {
it = it->next();
}
return it;
}
Node* head{nullptr};
std::size_t size_{0u};
};
template<typename T>
LinkedList<T>::LinkedList():
head{nullptr},
size_{0u}
{}
template<typename T>
LinkedList<T>::~LinkedList() {
clear();
}
template<typename T>
void LinkedList<T>::push_back(const T& data) {
Node* novo{new Node(data, nullptr)};
if (empty()) {
head = novo;
} else {
Node* last = end();
last->next(novo);
}
size_++;
}
template<typename T>
void LinkedList<T>::push_front(const T& data) {
Node* novo{new Node(data, head)};
if (novo == nullptr) {
throw std::out_of_range("Lista cheia");
}
head = novo;
size_++;
}
template<typename T>
void LinkedList<T>::insert(const T& data, std::size_t index) {
if (index > size()) {
throw std::out_of_range("Posição inválida");
} else if (empty() || index == 0) {
push_front(data);
} else {
Node* novo{new Node(data)};
if (novo == nullptr) {
throw std::out_of_range("Lista cheia");
}
Node* anterior = head;
for (auto i = 0u; i < index-1; ++i) {
anterior = anterior->next();
}
novo->next(anterior->next());
anterior->next(novo);
size_++;
}
}
template<typename T>
void LinkedList<T>::insert_sorted(const T& data) {
if (empty()) {
push_front(data);
} else {
Node* anterior = head;
auto index = 0u;
while (anterior->next() != nullptr && data > anterior->data()) {
index++;
anterior = anterior->next();
}
if (data > anterior->data()) {
insert(data, index+1);
} else {
insert(data, index);
}
}
}
template<typename T>
T LinkedList<T>::pop(std::size_t index) {
if (empty()) {
throw std::out_of_range("Lista vazia popLL");
}
if (index >= size_) {
throw std::out_of_range("Posição inválida");
} else if (index == 0) {
return pop_front();
} else {
Node* anterior = head;
for (auto i = 0u; i < index-1; ++i) {
anterior = anterior->next();
}
Node* atual = anterior->next();
T requested = atual->data();
anterior->next(atual->next());
delete atual;
size_--;
return requested;
}
}
template<typename T>
T LinkedList<T>::pop_back() {
return pop(size_-1);
}
template<typename T>
T LinkedList<T>::pop_front() {
if (empty()) {
throw std::out_of_range("Lista vazia popfrontLL");
}
Node* atual = head;
head = head->next();
T requested = atual->data();
delete atual;
size_--;
return requested;
}
template<typename T>
void LinkedList<T>::remove(const T& data) {
if (empty()) {
throw std::out_of_range("Lista vazia removeLL");
}
if (contains(data))
pop(find(data));
}
template<typename T>
std::size_t LinkedList<T>::find(const T& data) const {
Node* anterior = head;
auto index = 0u;
while ( (index < size_) && (data != anterior->data()) ) {
index++;
anterior = anterior->next();
}
return index;
}
template<typename T>
void LinkedList<T>::clear() {
Node* anterior = head;
Node* atual = head;
while (atual != nullptr) {
anterior = atual;
atual = atual->next();
delete anterior;
}
head = nullptr;
size_ = 0u;
}
template<typename T>
std::size_t LinkedList<T>::size() const {
return size_;
}
template<typename T>
bool LinkedList<T>::empty() const {
return (size_ == 0);
}
template<typename T>
bool LinkedList<T>::contains(const T& data) const {
return (find(data) >= 0 && find(data) < size_);
}
template<typename T>
T& LinkedList<T>::at(std::size_t index) {
if (empty()) {
throw std::out_of_range("Lista vazia atLL");
}
if (index >= size_) {
throw std::out_of_range("Posição inválida");
} else if (index == 0) {
return head->data();
} else {
Node* atual = head;
for (auto i = 0u; i < index; ++i) {
atual = atual->next();
}
return atual->data();
}
}
template<typename T>
T& LinkedList<T>::at(std::size_t index) const {
if (empty()) {
throw std::out_of_range("Lista vazia atconst LL");
}
if (index >= size_) {
throw std::out_of_range("Posição inválida");
} else if (index == 0) {
return head->data();
} else {
Node* atual = head;
for (auto i = 0u; i < index; ++i) {
atual = atual->next();
}
return atual->data();
}
}
#endif