Chained lists

Asked

Viewed 121 times

0

I’m tending to create a program that as I enter with the values, it adds at the beginning of the chained list, so I saw it holding the values just right, but when printing it does not print, as if the list is empty, I looked for the error and could not find but I think it is related to the pointer in the insert function, can anyone show me the error? I really need to learn this 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;
    }
}
  • Try to put the idented code correctly. This will greatly facilitate the resolution of your problem.

  • then I don’t know how to use stackoverflow, if you can explain to me how I put the code straight I thank you

  • I tbm am not very experienced, but the way I do is to go in editing and identar on the same hand. Sometimes it’s worth it to take your code and put it in a text edit, come up with spaces (not tabs) and paste here.

  • is that I had seen that if I used CTRL+K he would do this, but only now that I saw that I have to select the text and then press the shortcut, now it’s set straight

  • detail plus error... occurs when you do what? What shows in error message?

  • edited there, but I think the pointer ta pointing to the wrong place, but as I am not very experienced I can not identify

Show 1 more comment

2 answers

0


There’s a lot of things you need to fix:

  • To be able to insert you have to pass a pointer to the pointer of the main here:

    int inserir(celula **ini);
    

    Otherwise you won’t get the pointer on main when he’s the NULL. This even corresponds to the call already being made (correctly):

    aux = inserir(&inicio);
    
  • Once the function escreve do not need to change the list can pass the pointer normally without being the address of the pointer as this to do:

    escreve(&inicio); //deve ser apenas escreve(inicio);
    
  • In function insere you must now receive the address of the main and change its value with particular attention when it is NULL, which is the starting value.

  • When printing the list:

    while(aux->prox != NULL) 
    

    Must travel until the pointer is NULL and not the ->prox!=NULL, otherwise the last element will not be shown.

Applying all these changes the code 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 statements that I had as:

celula * aux;

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

To

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

To make code shorter and more readable.

  • Can you explain to me what these operators are for? (two points and "?")

  • @Guilhermegarcia This is basically a ternary operator, a if in a single line, which ends up being shorter. A simple example would be int x = a == 2? 10:20;. If a for 2, x gets 10 otherwise you get 20. In response the aux->prox = ini == NULL ? NULL : *ini; is interpreted as: aux->prox gets NULL if ini for NULL or you get *ini which is the value pointed out by ini

  • I managed to understand, really so it becomes simpler to use

0

I’ll try to help:

SHORT answer, follow the corrected code, put /// on the lines on which I made some change:

    #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;
        }
    }

LONG answer, I will try to explain the problem.

Take a look at the following lines:

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

Here I don’t know if you knew what you were doing calling the address from a pointer and just forgot to put an asterisk in the insert() function, or if you put & hoping to work (I do this sometimes...). Counted q the second case is true the problem appears in your note ja q vc starts the pointer as null and in the first call insert your inserir(inicio) is equal to inserir(NULL), and aew its task ini = aux; no longer makes sense.

Next problem ta no write, this line

 while(aux->prox != NULL)

asks if the next one is null, which does q every time you print the first entered value will not appear

I guess that’s it, I hope I helped, good luck aew

Browser other questions tagged

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