forked from EddyVegaGarcia/TP-n-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistaCircularCursor.cpp
119 lines (72 loc) · 2.66 KB
/
listaCircularCursor.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
#include "listaCircularCursor.h"
template<class T> listaCircularCursor<T>::ListaCircularCursor(){
this->cursor = NULL;
this->primero = NULL;
this->tamanio = 0;
}
template<class T> void listaCircularCursor<T>::insertar(T elementoNuevo){
this->insertar(elemento, this->tamanio+1);
}
template<class T> void listaCircularCursor<T>::insertar(T elementoNuevo, unsigned int posicion){
if ((posicion > 0) && (posicion <= this->tamanio + 1)) {
Nodo<T>* nuevo = new Nodo<T>(elemento);
if (posicion == 1) {
nuevo->cambiarSiguiente(this->primero);
this->primero = nuevo;
//para que el ultimo apunte al nuevo primero
Nodo<T>* ultimo = this->obtenerNodo(this->tamanio);
ultimo->cambiarSiguiente(nuevo)
} else {
Nodo<T>* anterior = this->obtenerNodo(posicion - 1);
nuevo->cambiarSiguiente(anterior->obtenerSiguiente());
anterior->cambiarSiguiente(nuevo);
}
this->tamanio++;
this->inicializarCursor(); //retorna el cursor al principio de la lista
}
}
template<class T> void listaCircularCursor<T>::remover(unsigned int posicion){
if ((posicion > 0) && (posicion <= this->tamanio)) {
Nodo<T>* removido;
if (posicion == 1) {
removido = this->primero;
this->primero = removido->obtenerSiguiente();
Nodo<T>* ultimo = obtenerNodo(this->tamanio);
ultimo->cambiarSiguiente(this->primero);
} else {
Nodo<T>* anterior = this->obtenerNodo(posicion - 1);
removido = anterior->obtenerSiguiente();
anterior->cambiarSiguiente(removido->obtenerSiguiente());
}
delete removido;
this->tamanio--;
this->iniciarCursor(); //retorna el cursor al principio de la lista
}
}
template<class T> void listaCircularCursor<T>::inicializarCursor(){
this->cursor = NULL;
}
template<class T> void listaCircularCursor<T>::avanzarCursor(){
if (this->cursor == NULL) {
this->cursor = this->primero;
} else {
this->cursor = this->cursor->obtenerSiguiente();
}
}
template<class T> T listaCircularCursor<T>::obtenerCursor(){
T elemento;
if (this->cursor != NULL) {
elemento = this->cursor->obtenerDato();
}
return elemento;
}
template<class T> void listaCircularCursor<T>::estaVacia(){
return (this->tamanio == 0);
}
template<class T> listaCircularCursor<T>* Lista<T>::obtenerNodo(unsigned int posicion) {
Nodo<T>* actual = this->primero;
for (unsigned int i = 1; i < posicion; i++) {
actual = actual->obtenerSiguiente();
}
return actual;
}