Editing in Linked Lists

Asked

Viewed 70 times

0

I have a college protégé for a programming chair. One of the objectives is the evaluation of inserted projects (the insertion part I already have working), my problem is that when evaluated, I don’t know how but he eliminates the list. Below is my role to insert the reviews:

No * avaliarProjetos(No * head){

    int projeto;

    cout<<"Introduza o codigo do projeto\n";

    cin >> projeto;

    while (head != NULL)
    {

        if (head->codigo == projeto){

             int avaliacao;

             cout<<"Introduza a avaliacao do projeto \n";

             cin >> avaliacao;

             head->avaliacao = avaliacao;

        }

            head = head->prox;

    }

    return head;

}

Admit the following structure:

typedef struct dados {

    int codigo;

    string titulo;

    string instituicao;

    string investigador;

    int meses;

    string palavraschave[5];

    float financiamento;

    float subsidio;

    int avaliacao;

    float mediaAvaliacoes;

    struct dados *prox; //Apontador para o próximo

}No;

I thank you in advance for your help.

  • Apparently there are no problems in the presented code. The problem must be in the call to the function. As you are calling this function?

1 answer

0


The problem is that you return the pointer to the list you received -and it is pointing to the end of the list at the end of the run. This pointer to the end of the list is effectively an empty list (since it points to NULL):

No * avaliarProjetos(No * head){
    ...

    while (head != NULL)
    {...
            head = head->prox;
    }
    return head;
}

So, in this function there is no error - but as Vinicius pointed out in the comment, you’re probably calling her with something like:

no *minhalista; ... earthworm = insert Projects(); ... path_list = pathProjects(path_list);

And by doing the attribution with the value returned by "evaluationProjects" you are leaving minhaLista with null.

The function avaliarProjetos modifies the "inplace" records - you will not return a new list - so it may have the return type void - and you just do avaliarProjetos(minha_lista); and never minha_lista = avaliarProjetos(minha_lista) (replacing the previous value of the variable).

Browser other questions tagged

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