Insert linked list node

0

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.

    
asked by anonymous 25.04.2017 / 14:16

2 answers

1
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);
    
25.04.2017 / 14:33
0
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.

    
25.04.2017 / 19:58