Print circular list function

0

Personal I have a question of where and how should I change to this function to print circular list is correct.

Below is a print function from a linked list:

void imprime_lista(tipo_lista* p)
{
while (p != NULL)
{
    printf("%d\n", p->info);
    p = p -> prox;
}
printf("\n");
}
    
asked by anonymous 17.05.2017 / 21:11

1 answer

2

It only changes that the repeat loop stop criterion will be when it reaches the first loop. It is also necessary to change while to do , because the list already starts in the first element and with while will not print anything.

void imprime_lista(tipo_lista* p)
{
    if(p==NULL)
        return;
    tipo_lista* primeiro = p;
    do
    {
        printf("%d\n", p->info);
        p = p -> prox;
    }while(p != primeiro);
    printf("\n");
}
    
17.05.2017 / 21:31