LISTAS
Funciones de manipulación
struct Lista { int Contenido; struct Lista *Siguiente; }; /* Prototipos de funciones de manipulación */ struct Lista *InsertaInicio(struct Lista *L, int Elemento); struct Lista *InsertaFinal(struct Lista *L, int Elemento); struct Lista *RemueveInicio(struct Lista *L, int *PElemento); struct Lista *RemueveFinal(struct Lista *L, int *PElemento); /* Implementación de las funciones de manipulación */ struct Lista *InsertaInicio(struct Lista *L, int Elemento) { struct Lista *NuevoNodo = malloc(sizeof(struct Lista)); NuevoNodo -> Contenido = Elemento; NuevoNodo -> Siguiente = L; return NuevoNodo; } struct Lista *InsertaFinal(struct Lista *L, int Elemento) { if(L == NULL) { L = malloc(sizeof(struct Lista)); L -> Contenido = Elemento; L -> Siguiente = NULL; } else L -> Siguiente = InsertaFinal(L -> Siguiente, Elemento); return L; } struct Lista *RemueveInicio(struct Lista *L, int *PElemento) { struct Lista *NuevaLista; if(L == NULL) return NULL; NuevaLista = L -> Siguiente; *PElemento = L -> Contenido; free(L); return NuevaLista; } struct Lista *RemueveFinal(struct Lista *L, int *PElemento) { if(L == NULL) return NULL; if(L -> Siguiente == NULL) { *PElemento = L -> Contenido; free(L); return NULL; } L -> Siguiente = RemueveFinal(L -> Siguiente, PElemento); return L; }
ICOM