Chained List (insertion at the end)

0

I'm trying to make a linked list with insert at the end of it, but at the time of writing the error and I do not know what the problem is.

#include <stdio.h>
#include <stdlib.h>

struct cel
{
    int v;
    struct cel *prox;
};
typedef struct cel celula;

int main()
{
    celula *inicio;
    inicio = NULL;
    inserefim(&inicio);
    escreve(inicio);
    return 0;
}

void inserefim(celula **ini)
{
    celula *aux;
    celula **fim;
    int x;
    do{
        scanf("%d",&x);
        if(x)
        {
            if(*ini == NULL){
                aux =(celula*) malloc(sizeof(celula));
                aux->v = x;
                aux->prox = NULL;
                *ini = aux;
                *fim = aux;
            }else{
                aux = (celula*)malloc(sizeof(celula));
                aux->v = x;
                (*fim)->prox = aux;
                aux->prox = NULL;
            }

        }
    }while(x);
}

void escreve(celula *ini)
{
    while(ini =! NULL)
    {
        printf("\nNUM:%d",ini->v);
        ini= ini->prox;
    }
}
    
asked by anonymous 06.02.2018 / 23:50

1 answer

0

In function inserefim you have a few things to do:

  • Missing in celula **fim; return pointer else to fim :

    if(*ini == NULL){
        ...
    }else{
        ...
        //aqui
    }
    

    Also note that the code you have in aux and if is a large part repeated. When that is the case you should try to put what is equal before or after else so as not to repeat itself and make the code simpler.

Then write your function if ... else like this:

void inserefim(celula **ini)
{
    celula *aux;
    celula *fim; //ponteiro simples
    int x = 1;
    do{
        scanf("%d",&x);
        if(x)
        {
            aux =(celula*) malloc(sizeof(celula)); //era igual no if else
            aux->v = x; //era igual no if else
            aux->prox = NULL; //era igual no if else

            if(*ini == NULL){
                *ini = aux;
            }else{
                fim->prox = aux;
            }

            fim = aux; //agora executa tanto no if como no else
        }
    }while(x);
}

In the inserefim function, you also made a mistake in the different escreve operator:

while(ini =! NULL)
// -------^ está =! quando devia estar !=

This type of distraction is very easy to catch in the compiler. Now look at the warning that he gives in this case with the different operator written backwards:

  

|| === Build: Debug in Test (compiler: GNU GCC Compiler) === |

     

... main.c || In function 'writes': |

     

... main.c | 13 | warning: assignment   makes pointer from integer without cast [enabled by default] |

     

... main.c | 13 | warning: suggest parentheses around assignment used as   truth value [-Wparentheses] |

     

|| === Build finished: 0 error (s), 2 warning (s) (0 minute (s), 1 second (s)) === |

This reinforces the idea that it is very important to look at the given warnings. When you are starting or when you do not have much experience yet you want to consider all warnings as mistakes even though in fact most of them are things you are not doing correctly, and that will have implications later.

View the code with the changes indicated in Ideone

    
07.02.2018 / 00:25