Editing in Linked Lists

0

I have a protection for the University for a programming chair. One of the goals is the evaluation of inserted projects (the insertion part is already working), my problem is that when I evaluate , I do not know how but it deletes the list >. Below is my function for entering the ratings:

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;

Thank you in advance for your help.

    
asked by anonymous 24.05.2015 / 02:11

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

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

No * myList;    ...    myList = insertProjects ();    ...    my_list = evaluateProjects (my_list);

And when doing the assignment with the value returned by "evaluating Projects" you are leaving minhaLista with NULL.

The avaliarProjetos function modifies the "inplace" records - you will not return a new list - then it can have the return type void - and you simply avaliarProjetos(minha_lista); and never minha_lista = avaliarProjetos(minha_lista) (which replaces the previous value of the variable).

    
25.05.2015 / 15:56