Insert function in list

1

Hello. I'm developing this function below:

void inserir_inicio (tipo_lista * p, tipo_lista * novo_no)
{
  novo_no -> prox = p;
  p = novo_no;
}

And in main I'm calling:

inserir_inicio(&p, cria_no(1));
imprimir_lista(p);

And when I run nothing comes out on the screen.

Can anyone tell me if the insert_target function is wrong?

    
asked by anonymous 22.05.2017 / 15:07

1 answer

3

In main your function starts empty (NULL), then you call the insert function and it does not update the value of main, so the list is still empty. If the list is not empty, you will insert it at the beginning and it will not update on the main which is the first one, so the error continues.

What is missing is return:

tipo_lista * inserir_inicio (tipo_lista * p, tipo_lista * novo_no)
{
  novo_no -> prox = p;
  return novo_no;
}

And no main:

p = inserir_inicio(p, cria_no(1));
imprimir_lista(p);
    
22.05.2017 / 21:27