Inserting in a circular list

-1

I am having second thoughts about this code of the insert circular list function.

tipo_lista * inserir_inicio (tipo_lista * p, tipo_lista * novo_no)
{
 //Se a lista estiver vazia
 if(p==NULL)
     return novo_no;

 tipo_lista* r = p; //Para manter a referencia ao primeiro elemento
 while (p->prox != NULL)
 {
     p = p->prox;
 }
 p->prox = novo_no;
 return r;
}

It's not working properly and I'm not sure if it's in this line of NULL .

    
asked by anonymous 25.05.2017 / 16:13

1 answer

2

If the list should be circular only, there is no beginning or end. In this case, add the new one anywhere. The following code adds it between the one in the past as a parameter and its successor.

void inserir_inicio (tipo_lista * p, tipo_lista * novo_no)
{
 //Se a lista estiver vazia
 if(p==NULL)
     return;

 novo_no->prox = p->prox;
 p->prox = novo_no;
}

As I have not seen a reference to an earlier element, I assume the list is not doubly linked.

    
26.05.2017 / 21:34