-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlcursorlist.cpp
316 lines (266 loc) · 5.15 KB
/
dlcursorlist.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "dlcursorlist.h"
//Constructor. Recibe un entero que determina su capacidad.
DLCursorList::DLCursorList(int n)
{
capacity = n;
map = new Registry[capacity];
}
//Destructor.
DLCursorList::~DLCursorList()
{
for(int i = 0; i < capacity; i++)
{
if(map[i].datum)
delete map[i].datum;
}
delete[] map;
}
//Inserta un elemento a la lista.
bool DLCursorList::insert(Object* o, int i)
{
//Valida la posición. Si no es válida, corta y retorna false.
if(i < 0 || i > size)
return false;
//Valida que el objeto no sea nulo. Si lo es, corta y retorna false.
if(o == NULL)
return false;
//Si está llena, retornar false y romper el flujo.
if(size == capacity)
return false;
//Encuentra un nuevo espacio.
int neo = findNextSlot();
if(neo == -1)
{
return false;
}
else
{
//Ponemos el dato en el espacio designado.
map[neo].datum = o;
if(i == 0)
{
//Ahora hacemos que neo apunte a la cabeza.
map[neo].next = head;
//Hacemos que head también apunte a neo.
map[head].prev = neo;
//Convertimos el nuevo nodo en la cabeza.
head = neo;
}
else
{
//Creamos un temporal para recorrer la lista.
int tmp = head;
//Recorre la lista. Se detiene en i-1.
for(int j = 0; j < i-1; j++)
{
tmp = map[tmp].next;
}
//Hacemos que el nuevo apunte al anterior
//del lugar al que lo insertaremos.
map[neo].prev = tmp;
//Hacemos que neo apunte al siguiente de tmp.
map[neo].next = map[tmp].next;
//Ahora hacemos al next del anterior neo.
map[tmp].next = neo;
//Esta parte enlaza al nuevo con el anterior que le toca.
if(i < size)
{
map[map[neo].next].prev = neo;
}
}
size++;
return true;
}
}
//Consigue el índice del elemento especificado como parámetro.
int DLCursorList::indexOf(Object* object) const
{
//-1 por si no encuentra el elemento.
int i = -1;
if(size > 0)
{
for(int j = 0; j < capacity; j++)
{
if((map[j].datum)->equals(object))
{
i = j;
break;
}
}
}
}
//Retorna el objeto localizado en la posición especificada.
Object* DLCursorList::get(unsigned i) const
{
if(i < 0 || i >= size)
{
return NULL;
}
else
{
return map[i].datum;
}
}
//Retorna el elemento especificado y lo borra de la lista.
Object* DLCursorList::remove(unsigned i)
{
if(i < 0 || i >= size || size == 0)
{
return NULL;
}
else
{
Object* ret;
if(i == 0)
{
//Guardamos la cabeza como temporal.
int tmp = head;
//Guardamos el dato de tmp.
ret = map[tmp].datum;
//Desconectamos al dato del slot.
map[tmp].datum = NULL;
//Hacemos a head el siguiente de head.
head = map[tmp].next;
//Desconectamos al nuevo head del anterior.
map[head].prev = -1;
//Desconectamos al head anterior del nuevo.
map[tmp].next = -1;
}
else
{
//Creamos la temporal para recorrer.
int tmp = head;
//Recorremos el arreglo y nos detenemos en el que borraremos.
for(int j = 0; j < i; j++)
{
tmp = map[tmp].next;
}
//Guardamos el dato en la posición.
ret = map[tmp].datum;
//Nos deshacemos de la conexión con datum.
map[tmp].datum = NULL;
//Hacemos que el anterior se conecte al siguiente.
map[map[tmp].prev].next = map[tmp].next;
if(i != size-1)
{
//Hacemos que el siguiente se conecte al anterior.
map[map[tmp].next].prev = map[tmp].prev;
map[tmp].next = -1;
}
//Ahora borramos las conexiones.
map[tmp].prev = -1;
}
size--;
return ret;
}
}
/*
* Retorna la posición (en la lista) del objeto
* que está antes del especificado
* por el parámetro.
*/
int DLCursorList::prev(int i) const
{
if(i < 0 || i >= size || size == 0)
{
return -1;
}
else
{
return map[i].prev;
}
}
/*
* Retorna la posición (en la lista) del objeto
* que está después del especificado
* por el parámetro.
*/
int DLCursorList::next(int i) const
{
if(i < 0 || i >= size || size == 0)
{
return -1;
}
else
{
return map[i].next;
}
}
//Borra todos los elementos de la lista.
void DLCursorList::clear()
{
if(size != 0)
{
for(int i = 0; i < size; i++)
{
if(map[i].datum)
{
map[i].prev = -1;
map[i].next = -1;
delete map[i].datum;
}
}
size = 0;
}
}
//Retorna el primer elemento de la lista.
Object* DLCursorList::first() const
{
if(size == 0)
{
return NULL;
}
else
{
return map[head].datum;
}
}
//Retorna el último elemento de la lista.
Object* DLCursorList::last() const
{
if(size == 0)
{
return NULL;
}
else
{
int tmp = head;
for(int i = 0; i < size; i++)
{
tmp = map[tmp].next;
}
return map[tmp].datum;
}
}
//Imprime cada elemento en la lista.
void DLCursorList::print() const
{
int tmp = head;
for(int i = 0; i < size; i++)
{
map[tmp].datum->print();
tmp = map[tmp].next;
}
}
//Determina si la lista está llena o no.
bool DLCursorList::isFull() const
{
return (size == capacity);
}
//Encuentra el siguiente espacio dónde insertar.
int DLCursorList::findNextSlot() const
{
//Variable que guardará la posición libre.
//Inicializada en -1 por si no encuentra.
int ret = -1;
for(int i = 0; i < capacity; i++)
{
//Si encuentra espacio, lo guarda y rompe el ciclo.
if(!map[i].datum)
{
ret = i;
break;
}
}
return ret;
}