How to relate two list-type data structures

Asked

Viewed 153 times

3

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

In my developments I can perform the enrollment of students but I stop at the time of making the relationship and appointing students to books at the time I decide to register the books.

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

Code


Addenda to improve understanding

Cada aluno possui sua própria lista de livros construída por uma struct

  • 4

    I wanted to comment on your publication, but I don’t have enough reputation. So this isn’t an answer, I just want to understand your question. You have a chain list of students, right? And each student is related to some books. And each student will be connected to a chained list of books, that’s it?

  • Exactly that Gabriel, I just added an image to better convey this idea.

1 answer

2


I’ll help you from that response channel, okay?

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

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 doesn’t need to be done, you should use an if/Else. The "Cot" variable is dispensable, and your final IF is never printed because if you didn’t 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.");
}

And well, as for the problem of the chained list within another chained list, I’ve worked with that already in some labs, and I haven’t followed your idea of structures. You set up four structs, two for lists and two for us. The way I learned and did very well doing uses only the nodes, and to increase the efficiency of the list uses the technique of nodes "dummy", are nodes that have no function and are the first on the list, and serve, for example, to facilitate removals and insertions the first time they occur. (I recommend reading: https://www.ime.usp.br/~pf/algorithms/classes/list.html)

Also, as you want to work with removals and inserts at the beginning and end, I suggest using a control struct, which contains the start and end positions of the list.

Thus, 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;
}
  • Thank you for seeing the other errors that were present and for passing me this material, I will read and modify what needs to be modified. I do not understand very well the use of Control, but I believe that the material will answer my questions, anything I contact.

Browser other questions tagged

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