I have this function to insert in the beginning, but I am not able to test it. Following:
void inserir_inicio (tipo_lista * p, tipo_lista * novo_no)
{
novo_no -> prox = p;
p = novo_no;
}
I want to identify the error of this function.
I have this function to insert in the beginning, but I am not able to test it. Following:
void inserir_inicio (tipo_lista * p, tipo_lista * novo_no)
{
novo_no -> prox = p;
p = novo_no;
}
I want to identify the error of this function.
tipo_lista * inserir_inicio (tipo_lista * p, tipo_lista * novo_no)
{
novo_no -> prox = p;
return novo_no;
}
The new_no will now be the first element, just return it. When you call the function, your list should get the function return.
Ex:
p = tipo_lista inserir_inicio (p, novo_no);
void inserir_inicio(tipo_lista *p, tipo_lista *novo_no){
novo_no->prox = &(*p);
*p = *novo_no;
}
It seems to me that you forgot to do the dereferencing of the p-pointer. One of the ways I enjoy analyzing function parameters is to see them as if they were variables declared implicitly within the function. For example, in your case, you expect the address of a list_type variable to modify it using a pointer named p
. But you have forgotten that p is a local variable of the insert_initial function, so when you say p = novo_no
you are modifying the local variable, not the target variable. So you dereference pe novo_no: To say that you want to modify and get their memory addresses, respectively.