-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
193 lines (170 loc) · 4.43 KB
/
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
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include "node.h"
#include "iterator.h"
using namespace std;
template<typename T>
class List {
private:
Node<T> *head;
Node<T> *tail;
int nodes;
void print_reverse(Node<T> *head);
public:
List() : head(NULL), tail(NULL) {};
T front() {
if (head == NULL) {
return NULL; // Deberías retornar una excepción (throw, no null)
} else {
return head->data;
}
};
T back() {
if (head == NULL) {
return NULL; // Igual que el caso anterior
} else {
Node<T> *Tmp = head;
while (Tmp->next) {
Tmp = Tmp->next;
}
return Tmp->data;
}
};
void push_front(T value) {
Node<T> *Tmp = new Node<T>;
Tmp->data = value;
if (head == NULL) {
head = Tmp;
} else {
Tmp->next = head;
head = Tmp;
}
};
void push_back(T value) {
Node<T> *NewNode = new Node<T>;
NewNode->data = value;
if (head == NULL) {
head = NewNode;
} else {
Node<T> *Tmp = head;
while (Tmp->next) {
Tmp = Tmp->next;
}
Tmp->next = NewNode;
}
};
void pop_front() {
Node<T> *Tmp = head; // No estás controlando el caso vacío
head = Tmp->next;
delete Tmp;
Tmp = nullptr;
};
void pop_back() {
Node<T> *Tmp = head; // Igual que el caso anterior
while (Tmp->next->next) {
Tmp = Tmp->next;
}
delete Tmp->next;
Tmp->next = nullptr;
};
T get(int position) {
if (position < 0) {
// TODO: Throw accurate exception: Index out of range
return NULL; // Deberían ser excepciones, no NULLS
} else if (head == NULL) {
// TODO: Throw accurate exception: List empty
return NULL;
} else if (position == 0) {
return head->data;
} else {
int aux = 0;
Node<T> *Tmp = head;
while (Tmp) {
if (position == aux) {
break;
}
Tmp = Tmp->next;
aux += 1;
}
if (position > aux) {
// TODO: Throw accurate exception: Index out of range
return NULL;
}
return Tmp->data;
}
};
void concat(List<int> *other) { // Podrías usar tail en tu implementación para mejorar los tiempos
if (!this->empty()) {
if (!other->empty()) {
Node<T> *Tmp = head;
while (Tmp->next) {
Tmp = Tmp->next;
}
Tmp->next = other->head;
} // En caso que la otra lista esté vacía, no hago nada :)
} else {
head = other->head;
}
};
bool empty() {
return head == NULL;
};
int size() { // Podrías usar el int nodes para mejorar los tiempos
int r = 0;
if (head == NULL) {
return r;
} else {
Node<T> *Tmp = head;
while (Tmp) {
r += 1;
Tmp = Tmp->next;
}
return r;
}
};
void print() {
if (head == NULL) {
cout << "Empty list" << endl;
} else {
Node<T> *Tmp = head;
while (Tmp) {
cout << Tmp->data << " ";
Tmp = Tmp->next;
}
}
};
void print_reverse() {
Node<T> *NTmp = head;
List<T> *LTmp = new List<T>; // Podrías usar un array en vez de una lista para mejorar los tiempos
while (NTmp) {
LTmp->push_front(NTmp->data);
NTmp = NTmp->next;
}
LTmp->print();
LTmp->clear();
};
void clear() {
if (head != NULL) {
Node<T> *iNode = head;
while (iNode) {
Node<T> *toDelete = iNode;
iNode = iNode->next;
delete toDelete;
toDelete = nullptr;
}
delete iNode;
iNode = nullptr;
head = NULL;
}
};
Iterator<T> begin();
Iterator<T> end();
~List() {
if (head) {
head->killSelf();
}
head = NULL;
}
};
#endif