Print on dynamic list screen

Asked

Viewed 438 times

1

Staff can not perform the function of printing on the screen, could help me in this function below.

void imprime_lista(Lista* li){
 while (li != NULL)
 {
    printf("%d ", li -> struct amatricula);
    li = li -> prox;
 }
 printf("\n");
}

The structures are like this:

struct aluno {
  int matricula;
  char nome[30];
  float n1, n2, n3;
};
typedef struct elemento* Lista;

//Arquivo ListaDinEncad.c
struct elemento {
  struct aluno dadosAlunos;
  struct elemento *prox;
};
typedef struct elemento Elem;

Lista* criar_lista();

Main:

int main()
{
int varAux = 0;

struct aluno estruturaAlunoAuxiliar;
Lista *listaDeAlunos;

listaDeAlunos = criar_lista();

estruturaAlunoAuxiliar.matricula = 1;
strcpy(estruturaAlunoAuxiliar.nome, "Thiago Ferreira");
estruturaAlunoAuxiliar.n1 = 7;
estruturaAlunoAuxiliar.n2 = 6;
estruturaAlunoAuxiliar.n3 = 9;

varAux = inserir_no_inicio_da_lista (listaDeAlunos, estruturaAlunoAuxiliar);
//    varAux = imprime_lista(listaDeAlunos);
//printf("%d\n", varAux);
imprime_lista(listaDeAlunos);
return 0;
  • I imagine there’s some error in the structures as well, but it’s been a long time since I’ve made a list in C and I won’t remember. Only, to print something on imprime_lista, I imagine, you should change li -> struct amatricula for li->dadosAluno.matricula

1 answer

3


If the dadosAluno is not a pointer so is accessed with ..

The printing function should then be:

void imprime_lista(Lista* li){
    while (li != NULL)
    {
        printf("Nome: %s ", li->dadosAluno.nome /*<--diferente aqui*/);
        li = li -> prox;
    }
    printf("\n");
}

Notice it started with -> because li is a pointer. If it were not(although in these cases it is necessary that it be so that it can end in NULL) would be li.dadosAluno.nome.

If the dadosAluno had been declared as a pointer, thus:

struct elemento {
  struct aluno *dadosAlunos;
  struct elemento *prox;
};

Then the access in the function would already be like this:

li->dadosAluno->nome

However this would imply allocating this object with malloc before using and removing when not needed with the free, which would considerably complicate the code.

Edit:

The mistake you’re making:

request for Member 'dataAlunos' in Something not a Structure or Union

It’s actually about a subtle error in the code here:

void imprime_lista(Lista* /*<--aqui*/ li){

The parameter shall be Lista li, and not Lista* li , for Lista is already a typedef for a pointer, as you can see here:

typedef struct elemento* Lista;

Soon I was using a pointer to a pointer! As if it were struct elemento**

Edit 2:

Just like my last appointment, Lista is already a pointer to a list node, so you’re not supposed to use Lista* nowhere, unless it is as a parameter of a function that changes the start of the list.

This makes yours main should be:

int main()
{
    int varAux = 0;

    struct aluno estruturaAlunoAuxiliar;
    Lista listaDeAlunos; //sem Lista* agora

    listaDeAlunos = criar_lista(); //este agora deve devolver um Lista e não Lista*

    estruturaAlunoAuxiliar.matricula = 1;
    strcpy(estruturaAlunoAuxiliar.nome, "Thiago Ferreira");
    estruturaAlunoAuxiliar.n1 = 7;
    estruturaAlunoAuxiliar.n2 = 6;
    estruturaAlunoAuxiliar.n3 = 9;

    //deve rever esta função pois ela para estar bem implementada deveria receber um 
    //Lista* para poder modificar o inicio da lista, o que faz com que deva ser passado o 
    //endereço da lista aqui no main
    varAux = inserir_no_inicio_da_lista (&listaDeAlunos, estruturaAlunoAuxiliar);

    imprime_lista(listaDeAlunos); //imprime agora recebe Lista e não Lista*

    return 0;
}

You will now need to adjust the respective functions to match this new main in terms of parameters and returns.

  • From: request for Member 'dataAlunos' in Something not a Structure or Union|

  • He even compiled but how it would look in main: print_list(listDelunos);

  • @André Depends on how the list was declared on main. You’d have to see that part of the code

  • I edited with the main

  • @Andrew has already edited the answer to contemplate his main also

  • It gives an error that the program stopped working. But enough print yes.

  • 1

    @Andre, this will have to do with all the other functions you have. If you can’t visualize and/or solve these problems I advise you to open a new question, as it already escapes a lot of what you did here

  • Let me ask another question about insertion

Show 4 more comments

Browser other questions tagged

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