How to relate two data structures of the list type

3

I am developing a project to better understand data structure, in the project is being used data structure of type list and in it I must register students and after the registered, I can choose any student that is registered to include a quantity n of books, these books are also ordered by a own structure that always points to the next or final.

In my development I am able to register students, but it is difficult to make the relationship and the students' attention to books at the moment when I decide to register the books.

I am making available all source code developed, it is all commented on having an easy navigation of what is being done, but specifically the creation of the book is on line 355.

Code

Addendums to improve understanding

    
asked by anonymous 05.01.2017 / 15:09

1 answer

2

I'll help you from this channel, okay?

You commented that you have an error on line 178 of your code. Your problem is not updating the value of aux, meaning your while-statement never stops because aux never changes value. The idea would be as follows:

while(aux != NULL) {

    if(cod_aluno == aux->aluno.codigo) { 
         if(aux->inicLivro != NULL)
            inicio = removeOrdenado(inicio, cod_aluno);
         else
             printf("O aluno não pode ser removido, pois possui livros em sua conta.");
    }
    else 
         aux = aux->proximo;

}       

Now, reading the bug reported on line 222, you go through the list, and even if you think, you update aux, which does not need to be done, you should use an if / else. The variable "cot" is expendable, and its final IF is never printed because if you did not find that code, the value of aux is NULL. The code should be:

struct listaAluno *aux;
int cod_aluno;

aux = inicio;


    if(aux == NULL)
{
    printf("Não há alunos cadastrados no sistema.");
    printf("\nImpossivel realizar consulta.");
}
else
{
    printf("\nLocalizar Aluno\n");
    printf("Informe o código do aluno: ");
    scanf("%i", &cod_aluno);

    while(aux != NULL)
    { 
        if(cod_aluno == aux->aluno.codigo)
        {
            printf("\nCódigo: %d | Nome: %s | Curso: %s",  aux->aluno.codigo, aux->aluno.nome, aux->aluno.curso);
        }
        else 
            aux = aux->proximo;
    }
}

if(aux == NULL)
{
    printf("O código informado não pertece a nenhum aluno cadastrado no sistema.");
}
Well, as for the threaded list problem within another threaded list, I've worked with it already in some labs, and I did not follow your idea of structures. You put together 4 structs, 2 for lists and 2 for us. The way I learned it and I did very well doing only the nodes, and to increase the efficiency of the list uses the technique of dummy nodes, are us that have no function and are the first of the list, and serve, for example , to facilitate removals and insertions the first time they occur. (I recommend reading: link )

In addition, since you want to work with removals and insertions at the beginning and end, I suggest using a control struct, which contains the start and end positions of the list.

So, in pseudocode, the structures would be:

struct Aluno { 
  informações do aluno;
  ponteiro para o próximo aluno;
  ponteiro para sua lista de livros;
}
struct Livro { 
 informações do livro;
 ponteiro para o próximo livro;
}
struct Controle {
 informações da lista (quantidade de nós, entre outros); //opcional
 ponteiro para o início da lista;
 ponteiro para o fim da lista;
}
    
05.01.2017 / 17:28