Error while printing static list

Asked

Viewed 214 times

6

How do I print my static list?

#include <stdio.h>
#include <stdlib.h>

#define MAX 3

struct alunos {
    int matricula;
    char nome[30];
    float nota1, nota2;
};

typedef struct lista {
    int quant;
    struct alunos dados[MAX];

} Lista;

Lista* cria_Lista() {

    Lista* l;
    l = (Lista*) malloc(sizeof(struct lista));

    if(l != NULL)
        l->quant = 0;

    return l;
}

void libera_Lista(Lista* l) {
    free(l);
}




int insere_final(Lista* l, struct alunos al) {

    if(l == NULL || lista_Cheia(l))
        return 0;

    l->dados[l->quant] = al;
    l->quant++;

    return 1;
}

int insere_inicio(Lista *l, struct alunos al) {

    if(l == NULL || lista_Cheia(l))
        return 0;

    int i;
    for(i= l->quant - 1; i>=0; i--) {
        l->dados[i + 1] = l->dados[i]; //proxima posicao e igual a atual
        l->dados[0] = al; //pega a primeira posicao
        l->quant++; //incrementa quantidade
        return 1;
    }

}

void imprime_lista(Lista* l) {

    if(l == NULL)
        return; // nao retorna nada

    int i; //variavel auxiliar
    for(i=0; i < l->quant; i++)
        printf("Matricula: %d\n", l->dados[i].matricula);
        printf("Nome: %s\n", l->dados[i].nome);
        printf("Notas: %f %f\n", l->dados[i].nota1, l->dados[i].nota2);


}



int main()
{
 struct alunos al[2] = {{3, "João", 8.45, 9.98},
  {1, "Maria", 6.75, 8.54}};

 Lista *l;
 l  = cria_Lista();

 int i;

 for(i=0; i<2; i++) {
        insere_inicio(l, al[0]);
 imprime_lista(l);

 }
libera_Lista(l);
    return 0;
}

You are printing empty values, I think you must be wrong in the statement after the main.

1 answer

2


There were so many mistakes in the code, I can’t even remember everyone I fixed. I will try to describe them by comparing what is in the question and what is left in mine that still has several problems, not tried to solve everything, just make it work.

I took the comments that were not useful and gave an organized and modernized.

The main error in printing is the lack of keys in the for. The ideal is always put key, even if you do not need, so avoid mistakes by carelessness.

The insertion function at the beginning did not insert anything, only made the displacement of the data, which even ended in the first interaction. I don’t think this is ideal and I don’t know if you’re doing it the right way.

#include <stdio.h>
#include <stdlib.h>

#define MAX 3

typedef struct alunos {
    int matricula;
    char nome[30];
    float nota1, nota2;
} Alunos;

typedef struct lista {
    int quant;
    Alunos dados[MAX];

} Lista;

Lista* cria_Lista() {
    Lista* l = malloc(sizeof(Lista));
    if (l != NULL) l->quant = 0;
    return l;
}

void libera_Lista(Lista* l) {
    free(l);
}

int insere_final(Lista* l, Alunos al) {
    if (l == NULL) { // || lista_Cheia(l)) { --- tirei porque a função não existe
        return 0;
    }
    l->dados[l->quant] = al;
    l->quant++;
    return 1;
}

int insere_inicio(Lista *l, Alunos al) {
    if (l == NULL) { // || lista_Cheia(l)) { --- tirei porque a função não existe
        return 0;
    }
    for (int i = l->quant - 1; i >= 0; i--) {
        l->dados[i + 1] = l->dados[i];
        l->dados[0] = al;
        l->quant++;
    }
    l->dados[0] = al;
    l->quant++;
    return 1;
}

void imprime_lista(Lista* l) {
    if (l == NULL) return;
    for(int i = 0; i < l->quant; i++) {
        printf("Matricula: %d\n", l->dados[i].matricula);
        printf("Nome: %s\n", l->dados[i].nome);
        printf("Notas: %f %f\n", l->dados[i].nota1, l->dados[i].nota2);
    }
}

int main() {
    Alunos al[2] = {{3, "João", 8.45, 9.98}, {1, "Maria", 6.75, 8.54}};
    Lista *l = cria_Lista();
    for (int i = 0; i < 2; i++) {
        insere_inicio(l, al[i]);
        imprime_lista(l);
    }
    libera_Lista(l);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • the full list function I hid from the code.

  • @user57116 Help put everything we can to test. Making a [mcve]. Take a look at [tour] too. You can vote for anything you want on the site.

Browser other questions tagged

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