I am try to delete a sub-struct in a two level linkedlist.
I am able to delete correct if i delete the 1º item, if i delete other items the list don´t work properly.
Code:
typedef struct SPECIES species, *pspecies;
typedef struct ANIMAL no, *pno;
struct SPECIES {
char especie[MAX];
char idalphanumeric[MAX];
long int nranimais;
pno listadeanimais;
pspecies prox;
};
struct ANIMAL {
char especie[MAX];
int id;
char nome[MAX];
int peso;
char nomelocal[MAX];
pno prox;
};
pspecies apaga_animal_zoo(pspecies list) {
pno aux, anterior;
int i = 1380;
while (list != NULL) {
aux = list->listadeanimais;
anterior = NULL;
while (aux != NULL) {
if (aux->id == i) {
if (anterior == NULL) { // Se for o primeiro animal, vou passar a cabeça da lista para o nó a seguir
list->listadeanimais = aux->prox;
return list;
} else {// Não sendo o animal da lista vou ligar o nó "n-1" ao nó "n+1", apagando o nó "n"
anterior->prox = aux->prox;
list->listadeanimais = aux->prox;
}
printf("The Animal with the name %s was remove\n", aux->nome);
free(aux);
return list;
//aux = aux->prox;
} else {
anterior = aux;
aux = aux->prox;
}
}
list = list->prox;
}
if (aux == NULL) { // The Animal don´t existe.
puts("Animal nao existe sera enviado para o MENU");
return list;
}
puts("The List is empty");
return list;
}