-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclase_lista_enlazada.hh
163 lines (159 loc) · 3.3 KB
/
clase_lista_enlazada.hh
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
#pragma once
#include <cassert>
#include "clase_nodo.hh"
/**
* @brief Clase Lista Simplemente Enlazada
* @tparam T
*/
template <typename T>
class Lista_SE {
public:
/// Constructores
Lista_SE() :
head_{nullptr} {} /// Constructor por defecto
Lista_SE(const T& data) {
head_ = new Nodo_SE<T>;
head_->data(data);
} /// Constructor estándar
// ListaEnlazada(); /// Constructor copia
/// Destructores
~Lista_SE() { head_ = nullptr; }
/// Métodos
void InsertHead(const T& dato) { head_ = new Nodo_SE<T>{head_, dato}; }
void EraseHead() {
Nodo_SE<T>* temp = head_->next();
delete head_;
head_ = temp;
}
void InsertAfter(Nodo_SE<T>* const prev, Nodo_SE<T>* const nodo) {
Nodo_SE<T>* temp = prev->next();
prev->next(nodo);
nodo->next(temp);
}
void EraseAfter(Nodo_SE<T>* const prev) {
Nodo_SE<T>* temp = prev->next()->next();
delete prev->next();
prev->next(temp);
}
bool Empty() const {
if (head_ == nullptr) {
return true;
} else {
return false;
}
}
Nodo_SE<T>* Find(const T& dato) {
Nodo_SE<T>* temp = head_;
while (temp->data() != dato) {
if (temp->next() != nullptr) {
temp = temp->next();
} else {
return nullptr;
}
}
return temp;
}
T erase_last() {
if (this->Empty()) {
throw "🧨";
}
Nodo_SE<T>* ptr{head_};
Nodo_SE<T>* prev{head_};
while (ptr->next() != nullptr) {
prev = ptr;
ptr = ptr->next();
}
auto copy = ptr->data();
if (ptr != head_) {
prev->next(nullptr);
}
delete ptr;
return copy;
}
void Intercambiar() {
assert(!this->Empty());
assert(head().next() != nullptr);
Nodo_SE<T> copy = head();
this->EraseHead();
this->InsertAfter(head(), copy);
}
/// Friend
/// Sobrecargas Operadores
/// Sobrecargas Conversiones
/// Getters
const Nodo_SE<T>& head() const { return *head_; }
/// Setters
protected:
private:
/// Atributos
Nodo_SE<T>* head_;
};
/**
* @brief Clase Lista Doblemente Enlazada
* @tparam T
*/
template <typename T>
class Lista_DE {
public:
/// Constructores
Lista_DE() :
head_{nullptr} {} /// Constructor por defecto
Lista_DE(const T& data) {
head_ = new Nodo_DE<T>;
head_->data(data);
tail_ = *head_;
} /// Constructor estándar
// ListaEnlazada(); /// Constructor copia
/// Destructores
~Lista_DE() {
head_ = nullptr;
tail_ = nullptr;
}
/// Métodos
void InsertHead(const T& dato) { head_ = new Nodo_DE<T>{head_, dato}; }
void EraseHead() {
Nodo_DE<T>* temp = head_->next();
delete head_;
head_ = temp;
}
void InsertAfter(Nodo_DE<T>* const prev, Nodo_DE<T>* const nodo) {
Nodo_DE<T>* temp = prev->next();
prev->next(nodo);
nodo->next(temp);
}
void EraseAfter(Nodo_DE<T>* const prev) {
Nodo_DE<T>* temp = prev->next()->next();
delete prev->next();
prev->next(temp);
}
bool Empty() const {
if (head_ == tail_) {
return true;
} else {
return false;
}
}
Nodo_DE<T>* Find(const T& dato) {
Nodo_DE<T>* temp = head_;
while (temp->data() != dato) {
if (temp->next() != nullptr) {
temp = temp->next();
} else {
return nullptr;
}
}
return temp;
}
/// Friend
/// Sobrecargas Operadores
/// Sobrecargas Conversiones
/// Getters
const Nodo_DE<T>& head() const { return *head_; }
const Nodo_DE<T>& tail() const { return *tail_; }
/// Setters
protected:
private:
/// Atributos
Nodo_DE<T>* head_;
Nodo_DE<T>* tail_;
};