Problem in implementing a linked list - list insertion

0

I'm trying to implement this implementation in C. I'm having two problems.

First: Inside the main, I log the data I want and when I enter the list I get a problem.

Second: My data reading function "print_dates" gives error when compiling. I am using the codeblocks and the following message appears "invalid type argument of '->'".

I partially understood that "->" is pointer signaling and that to access my record data is not necessary the pointer, since the node has been accessed and we are looking for the internal statistical information. However, I would like a more detailed explanation if possible.

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

    typedef struct registro_st{         // sequência de objetos do mesmo tipo
        char login[50];
        char nome[50];
        float valor;
        struct registro *prox;
    } registro;

    typedef struct nodo_st{
        registro dado;
    struct nodo *prox;
    } nodo;

    typedef struct Lista_st{
        nodo *cabeca;
        nodo *cauda;
    int tamanho;
    } lista;


    nodo* CriarNodo(registro p){

            nodo* n;
            n = (nodo*)malloc(sizeof(nodo));
            n->dado = p;
            n->prox= NULL;
    return n;
    }

    void criarLista(lista *l){
            return NULL;
    }

    void insere_ini(lista *l, registro dado){
        nodo* novo = (nodo*)malloc(sizeof(nodo));
            if(novo=NULL){
                return 0; //falta de espaço
            };
            novo->dado = dado;
            novo->prox = l->cauda; //antigo primeiro aponta para o próximo
            l->cauda = novo; // novo nodo recebe ponteiro para começo
            return novo;



    }

    //FUNÇÕES PARA UTILIZAR NO MAIN

    void imprime_nomes(nodo* lista){            // função que imprime os valores
        nodo* p;
            for(p = lista; p != NULL; p = p->prox){
                printf("Nome eh: %s\n", p->dado->nome);
            }
    }
    void criar_registro(registro *p){
        printf("Qual login para registro:\n");
        scanf("%s", &p->login);
        printf("Qual o nome do contato:\n");
        scanf("%s", &p->nome);
        printf("Qual valor para registrar:\n");
        scanf("%f", &p->valor);
    }


    int main(){

    registro p1_main;
    lista   p2_main;
    nodo p3_main;

    char escolha;

    printf("Gostaria de registrar contatos?\n");
    printf("Digite -->>!! sim!! <<-- para registrar e -->>!! não !!<<-- para sair.\n");
    scanf("%s", &escolha);

    criarLista(&p2_main);

    do  {
        criar_registro(&p1_main); //poderia usar esta forma alternativa registro *novoRegistro = criar_registro()

        insere_ini(&p2_main, p1_main);
        }

        while(escolha != "nao");
    }
    
asked by anonymous 22.07.2017 / 02:43

1 answer

1

Let's break it down.

First, in print_name the use of for in this case is a bad programming practice because you do not know how many elements the list will have. To do so, use while like this:

void imprime_nomes(nodo* lista) // função que imprime os valores
{ 
    nodo* p = lista;
    while (p)
    {
        printf("Nome eh: %s\n", p->dado->nome);
        p = p->prox;
    }
}

Second, the problem is in the declaration of the node_st structure. You should point the given member as a pointer, not as a given by itself. That is why I was giving the problem with the pointer arrow ( - > ), because the structure was not indicated as pointer the way it should. This will look like this:

typedef struct nodo_st
{
    registro * dado;
}

Third, the parameter used in the CreateNode must be a pointer, so it must have an asterisk before the variable name:

nodo* CriarNodo(registro * p)
{
        nodo* n;
        n = (nodo*)malloc(sizeof(nodo));
        n->dado = p;
        n->prox= NULL;
        return n;
}

The same thing happens in the insere_ini function. Correcting, it looks like this:

void insere_ini(lista *l, registro * dado)
{
    nodo* novo = (nodo*)malloc(sizeof(nodo));
        if(novo=NULL){
            return 0; //falta de espaço
        };
        novo->dado = dado;
        novo->prox = l->cauda; //antigo primeiro aponta para o próximo
        l->cauda = novo; // novo nodo recebe ponteiro para começo
        return novo;
}

Finally, it is also necessary to adjust this operation for the modifications made, indicating the arguments as pointers:

insere_ini(&p2_main, &p1_main);

This will still generate multiple Warnings that you will have to fix gradually. I recommend an in-depth study of pointers, as it is one of the most essential knowledge to program well in C.

    
22.07.2017 / 03:17