Last active
December 4, 2020 18:42
-
-
Save Florencia-97/f24c276cb09611f27f1f10bf54ce4f10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "lista.h" | |
#include <stdlib.h> | |
#include <stdbool.h> | |
struct nodo{ | |
void* dato; | |
struct nodo* sig; | |
} typedef nodo_t; | |
struct lista{ | |
nodo_t* primero; | |
nodo_t* ultimo; | |
size_t largo; | |
} typedef lista_t; | |
struct lista_iter{ | |
lista_t* lista; | |
nodo_t* actual; | |
nodo_t* ant; | |
} typedef lista_iter_t; | |
nodo_t* crear_nodo(void* dato){ | |
nodo_t* nodo_nuevo= malloc(sizeof(nodo_t)); | |
nodo_nuevo->dato=dato; | |
return nodo_nuevo; | |
} | |
//PRIMITIVAS LISTA ENLAZADA SIMPLE | |
lista_t *lista_crear(void){ | |
lista_t* lista = malloc(sizeof(lista_t)); | |
if (lista==NULL)return NULL; | |
lista->primero=NULL; | |
lista->ultimo=NULL; | |
lista->largo=0; | |
return lista; | |
} | |
bool lista_esta_vacia(const lista_t *lista){ | |
return lista->largo==0; | |
} | |
bool lista_insertar_primero(lista_t *lista, void *dato){ | |
nodo_t* nodo_nuevo = crear_nodo(dato); | |
if (nodo_nuevo==NULL)return false; | |
if (lista_esta_vacia(lista)){ | |
nodo_nuevo->sig=NULL; | |
lista->primero=nodo_nuevo; | |
lista->ultimo=nodo_nuevo; | |
} | |
else{ | |
nodo_nuevo->sig=lista->primero; | |
lista->primero=nodo_nuevo; | |
} | |
lista->largo++; | |
return true; | |
} | |
bool lista_insertar_ultimo(lista_t *lista, void *dato){ | |
nodo_t* nodo_nuevo = crear_nodo(dato); | |
if (nodo_nuevo==NULL)return false; | |
if(lista_esta_vacia(lista))lista_insertar_primero(lista,dato); | |
else{ | |
nodo_nuevo->sig=NULL; | |
lista->ultimo->sig=nodo_nuevo; | |
lista->ultimo=nodo_nuevo; | |
} | |
lista->largo++; | |
return true; | |
} | |
void* lista_borrar_primero(lista_t* lista){ | |
if (lista_esta_vacia(lista))return NULL; | |
void* dato = lista->primero->dato; | |
nodo_t* nodo_aux= lista->primero; | |
lista->primero=nodo_aux->sig; | |
free(nodo_aux); | |
lista->largo--; | |
if(lista_esta_vacia(lista))lista->ultimo==NULL; | |
return dato; | |
} | |
void *lista_ver_primero(const lista_t *lista){ | |
if(lista_esta_vacia(lista))return NULL; | |
return lista->primero->dato; | |
} | |
void *lista_ver_ultimo(const lista_t* lista){ | |
if(lista_esta_vacia(lista))return NULL; | |
return lista->ultimo->dato; | |
} | |
size_t lista_largo(const lista_t *lista){ | |
return lista->largo; | |
} | |
void lista_destruir(lista_t *lista, void (*destruir_dato)(void *)){ | |
while(lista_esta_vacia(lista)==false){ | |
if (destruir_dato!=NULL)destruir_dato(lista->primero->dato); | |
lista_borrar_primero(lista); | |
} | |
free(lista); | |
} | |
//PRIMITIVAS ITERADOR EXTERNO | |
lista_iter_t* lista_iter_crear(lista_t *lista){ | |
lista_iter_t* iter = malloc(sizeof(lista_iter_t)); | |
if (iter == NULL) return NULL; | |
iter->lista= lista; | |
if (iter->lista->largo==0){ | |
iter->actual=lista->ultimo; | |
} | |
else{ | |
iter->actual=lista->primero; | |
} | |
iter->ant=NULL; | |
return iter; | |
} | |
bool lista_iter_avanzar(lista_iter_t *iter){ | |
if(lista_iter_al_final(iter))return false; | |
iter->ant=iter->actual; | |
iter->actual=iter->actual->sig; | |
return true; | |
} | |
void* lista_iter_ver_actual(const lista_iter_t *iter){ | |
if(lista_iter_al_final(iter))return NULL; | |
return iter->actual->dato; | |
} | |
bool lista_iter_al_final(const lista_iter_t *iter){ | |
return iter->actual==NULL; | |
} | |
//Si pierdo memoria revisar acá | |
void lista_iter_destruir(lista_iter_t *iter){ | |
free(iter); | |
} | |
bool lista_iter_insertar(lista_iter_t *iter, void *dato){ | |
nodo_t* nodo_nuevo= crear_nodo(dato); | |
if(nodo_nuevo==NULL)return false; | |
if(lista_iter_al_final){ | |
nodo_nuevo->sig=NULL; | |
iter->ant->sig=nodo_nuevo; | |
iter->actual=nodo_nuevo; | |
iter->lista->ultimo=nodo_nuevo; | |
} | |
else if(iter->lista->largo==0){ | |
nodo_nuevo->sig=NULL; | |
iter->actual=nodo_nuevo; //se supone que ant sigue siendo null | |
iter->lista->primero=nodo_nuevo // o iter_actual?? | |
} | |
else{ | |
iter->ant=iter->actual; | |
nodo_nuevo->sig=iter->actual; | |
iter->actual=nodo_nuevo; | |
} | |
iter->lista->largo++; | |
return true; | |
} | |
void* lista_iter_borrar(lista_iter_t *iter){ | |
if(iter->actual==NULL && iter->ant==NULL)return NULL; | |
nodo_t* nodo_aux = iter->actual; | |
void* dato= iter->actual->dato; | |
iter->ant=iter->actual; | |
iter->actual=iter->actual->sig; | |
free(nodo_aux); | |
iter->lista->largo--; | |
return dato; | |
} | |
//PRIMITIVA ITER INTERNO | |
void lista_iterar(lista_t *lista, bool (*visitar)(void *dato, void *extra), void *extra){ | |
nodo_t* nodo_aux= lista->primero; | |
while(nodo_aux){ | |
visitar(dato,extra); | |
nodo_aux=nodo_aux->sig; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/void pruebas_iter_lista_vacia(void){ | |
printf("COMIENZO DE PRUEBAS ITER EXTERNO\n"); | |
printf("COMIENZO PRUEBAS ITER CON LISTA VACÍA\n"); | |
printf("Creo un lista y un iter con ella\n"); | |
lista_t* lista_4= lista_crear(); | |
lista_iter_t* iter_0= lista_iter_crear(lista_4); | |
print_test("Si la lista está vacía el actual del iterador es NULL",lista_iter_al_final(iter_0)==true); | |
print_test("No puedo avanzar el iterador",lista_iter_avanzar(iter_0)==false); | |
print_test("No puedo borrar elementos",lista_iter_borrar(iter_0)==NULL); | |
lista_iter_destruir(iter_0); | |
print_test("Se elimina el iter", true); | |
print_test("La lista sigue teniendo largo 0",lista_largo(lista_4)==0); | |
lista_destruir(lista_4,NULL); | |
print_test("Se elimina la lista", true); | |
} | |
void pruebas_iter_lista_con_elementos(){ | |
printf("COMIENZO PRUEBAS ITER CON LISTA CON ELEMENTOS\n"); | |
printf("Creo un lista con elemtnos\n"); | |
printf("Creo un iter con la lista\n"); | |
lista_t* lista_5= lista_crear(); | |
int a[3]; | |
int b[5]; | |
int c[2]; | |
int d[9]; | |
lista_insertar_ultimo(lista_5,a); | |
lista_insertar_ultimo(lista_5,b); | |
lista_insertar_ultimo(lista_5,c); | |
lista_iter_t* iter_1=lista_iter_crear(lista_5); | |
print_test("El actual al comenzar es el primero de la lista",lista_iter_ver_actual(iter_1)==a); | |
lista_iter_avanzar(iter_1); | |
print_test("Puedo avanzar correctamente el iterador",lista_iter_ver_actual(iter_1)==b); | |
lista_iter_avanzar(iter_1); | |
lista_iter_avanzar(iter_1); | |
print_test("No puedo avanzar cuando llegué al final de la lista",lista_iter_avanzar(iter_1)==false); | |
lista_iter_insertar(iter_1,d); | |
print_test("Agrego un elemento al final de la lista",lista_ver_ultimo(lista_5)==d); | |
lista_iter_destruir(iter_1); | |
print_test("Se elimina el iter", true); | |
lista_destruir(lista_5,NULL); | |
print_test("Se elimina la lista", true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment