Chained List (insert at end)

Asked

Viewed 2,186 times

0

I’m trying to make a linked list with insertion at the end of it, but at the time of writing the error and I don’t 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;
    }
}
  • And what is the error message?

1 answer

1


In function inserefim has some adjustments to make:

  • celula **fim; the end pointer does not need to be double, a single pointer is sufficient which simplifies the code

  • Missed in the else back point pointer fim for aux:

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

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

Then write your function inserefim thus:

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 function escreve also mistaken the different operator !=:

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

This type of distractions are very easy to catch in the compiler. Now see the Warning that it 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 a 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 warnings given. When it’s starting or when you don’t have much experience you want to consider all warnings as mistakes yourself, because in fact most of them are things that you’re not doing properly, and that will have implications later.

View the code with the changes indicated in Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.