1
I would like to do a function to remove the first element from a chained list, the way I did it is only removing the first node and removing the second element.
struct lista {
int info;
struct lista* prox;
};
typedef struct lista Lista;
Lista* removeInicio(Lista *l){
if(l->prox == NULL){
printf("Lista ja esta vazia\n");
return NULL;
}else{
Lista *tmp = l->prox;
l->prox = tmp->prox;
return tmp;
}
}
Edit: Solved with this algorithm.
Lista* removeInicio(Lista *l){
Lista* pointer = l->prox;
if(l->prox == NULL){
printf("Lista ja vazia\n\n");
return NULL;
}
l->prox = pointer-> prox;
free(pointer);
l->info--;}
The function it has gives perfectly to remove an element but it lacks the
free
and has to be called in the right way withminhaLista = removeInicio(minhaLista);
. A more idiomatic solution in C is the response of Claudio Lopes passing the pointer address and not doing based on returns.– Isac