Linked lists

0

I'm tending to create a program that as I go in with the values, it adds at the beginning of the list chained, so I saw it is storing the values right, but at the time of printing it not printa, as if the list it was empty, I looked for the error and I did not find but I think it is related to the pointer in the function of insert, can someone show me the error? I really need to learn that soon.

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

struct cel
{
    int valor;
    struct cel * prox;
};

typedef struct cel celula;

int inserir(celula *ini);
void escreve(celula *ini);

void main()
{
    int i,aux;
    celula *inicio;
    inicio = NULL;

    i=0;
    do
    {
        printf("\nLista[%d]:",i+1);
        aux = inserir(&inicio);
        printf("\n%d",aux);
        i++;
    } while (aux == 1);
    escreve(&inicio);
}

int inserir(celula *ini)
{
    int n;
    celula * aux;

    aux = (celula*) malloc(sizeof(celula));
    scanf("%d",&n);
    if(n == 0)
        return 0;
    aux->valor = n;
    aux->prox = ini;
    ini = aux;
    return 1;
}

void escreve(celula *ini)
{
    int i=1;
    celula * aux;

    aux=ini;
    while(aux->prox != NULL)
    {
        printf("\nlista[%d]=%d",i,aux->valor);
        i++;
        aux = aux->prox;
    }
}
    
asked by anonymous 02.02.2018 / 12:33

2 answers

0

There are several things you need to fix:

  • In order to do the insertion you have to pass a pointer to the main pointer here:

    int inserir(celula **ini);
    

    Otherwise you will not get the pointer in main when it is NULL . This even corresponds to the call that was already being made (correctly):

    aux = inserir(&inicio);
    
  • Since the escreve function does not need to change the list you can pass the pointer normally without being the address of the pointer as you are doing:

    escreve(&inicio); //deve ser apenas escreve(inicio);
    
  • / li>
  • When printing the list:

    while(aux->prox != NULL) 
    

    It should run until the pointer is insere and not main , otherwise the last element will not be shown.

Applying all these changes would look like this:

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

struct cel
{
    int valor;
    struct cel * prox;
};

typedef struct cel celula;

int inserir(celula **ini); //agora **
void escreve(celula *ini);

void main()
{
    int i = 0,aux;
    celula *inicio = NULL;

    do
    {
        printf("\nLista[%d]:",i+1);
        aux = inserir(&inicio);
        printf("\n%d",aux);
        i++;
    } while (aux == 1);
    escreve(inicio); //sem &
}

int inserir(celula **ini)
{
    int n;
    scanf("%d",&n);
    if(n == 0)
        return 0;

    //só cria se não for 0 para não alocar memoria não utilizada
    celula * aux = (celula*) malloc(sizeof(celula)); 

    aux->valor = n;
    aux->prox = ini == NULL ? NULL : *ini; //liga para o que veio do main ou NULL
    *ini = aux; //agora com * para modificar o ponteiro que veio do main

    return 1;
}

void escreve(celula *ini)
{
    int i=1;
    celula * aux = ini;

    while(aux != NULL) //sem usar o ->prox aqui
    {
        printf("\nlista[%d]=%d",i,aux->valor);
        i++;
        aux = aux->prox;
    }
}

See this example working on Ideone

I also chose to simplify some of the statements I had as:

celula * aux;

aux = (celula*) malloc(sizeof(celula));

To

celula * aux = (celula*) malloc(sizeof(celula));

To make the code shorter and more readable.

    
02.02.2018 / 14:02
0

I'll try to help:

Short answer, follow the corrected code, put /// in the lines where I made some changes:

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

    struct cel
    {
        int valor;
        struct cel * prox;
    };

    typedef struct cel celula;

    int inserir(celula **ini);///
    void escreve(celula *ini);

    void main()
    {
        int i,aux;
        celula *inicio;
        inicio = NULL;

        i=0;
        do
        {
            printf("\nLista[%d]:",i+1);
            aux = inserir(&inicio);
            printf("\n%d",aux);
            i++;
        } while (aux == 1);
        escreve(inicio);///
    }

    int inserir(celula **ini)
    {
        int n;
        celula * aux;

        aux = (celula*) malloc(sizeof(celula));
        scanf("%d",&n);
        if(n == 0)
            return 0;
        aux->valor = n;
        aux->prox = *ini;///
        *ini = aux;///
        return 1;
    }

    void escreve(celula *ini)
    {
        int i=1;
        celula * aux;

        aux=ini;
        while(aux != NULL)///
        {
            printf("\nlista[%d]=%d",i,aux->valor);
            i++;
            aux = aux->prox;
        }
    }

Answer LONG, I'll try to explain the problem.

Take a look at the following lines:

aux = inserir(&inicio);
.
.
int inserir(celula *ini)

Here I do not know if you knew what you were doing by calling the address of a pointer and just forgot to put an asterisk in the insert () function, in the hope of doing right (I do it sometimes ...). Contosted that the second case is true the problem appears in your note already q you start the pointer as null and in the first call to enter your inserir(inicio) is equal to inserir(NULL) , and aew its ini = aux; no longer makes sense.

The next problem does not write, this line

 while(aux->prox != NULL)

If the next one is nullo, what do you do every time you start the first value inserted will not appear

I think that's it, I hope it helped, good luck aew

    
02.02.2018 / 14:07