Inserting elements in a dynamic list

Asked

Viewed 299 times

1

I did this function, but I do not know if it is right, there is no error in the compilation, but it gives an error when the prompt that the program has stopped working.

Insertion at the beginning:

int inserir_no_inicio_da_lista (Lista* li, struct aluno al){
  if (li == NULL){
      return 0;
  }

  Elem* no;

  no = (Elem*) malloc(sizeof(Elem));

  if (no == NULL){
      return 0;
  }

  no -> dadosAlunos = al;
  no -> prox = (*li);

  *li = no;

  return 1;
}

Structures:

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();

Creates List:

Lista* criar_lista() {
  Lista* li;
  li = (Lista*) malloc(sizeof(Lista));

  if (li != NULL){
      *li = NULL;
  }
  return li;
}
  • WHAT MISTAKE IT MAKES ?

  • Also missing are the structs and typedefs so that everyone can understand how they are defined and understand the types that come in the function.

  • All right, I’ve edited for better understanding.

  • The code seems to be working for me. See here. How is the function criar_lista ? And how is the main ?

  • The main is equal to what is there in the link, the function I just added.

2 answers

1


The problem here is related to your previous question, and its Lista versus Lista*. The Lista for typedef is already a pointer criar_lista should return only Lista and not Lista*, otherwise it would be returning a pointer to a pointer.

Besides the logic is a little inconsistent. If the goal was to stay with NULL as a start, which is what usually makes the most sense, so just return NULL:

Lista* criar_lista() {
    return NULL;
}

Or assign NULL in the main directly that was better.

int main(){
    Lista listaDeAlunos = NULL;
}

If the goal was to create a knot it would have to do so:

Lista criar_lista() {
    //Lista e não Lista* | sizeof(Elem) e não sizeof(Lista)

    Lista li = (Lista) malloc(sizeof(Elem)); 

    return li;
}

Notice that it is sizeof(Elem) because we want to allocate space to a node. If it is sizeof(Lista) we are allocating space to a pointer that is not what is intended.

But this creates other problems for you, as you end up creating an empty, data-free node that will show the garbage you pick up in memory.

So my suggestion is that it affects directly on main the initial pointer of the list a NULL

Example working on Ideone with NULL in the main

0

Follow your code with some changes:

  • created a struct for the list, having a pointer to the first Elem of the list
  • I modified an excerpt of your insertion because he made wrong notes
  • I created a method to display the list

struct on the list:

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

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

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

typedef struct lista {
    Elem *primeiro;
} Lista;

void criar_lista(Lista *list) {
    list->primeiro = NULL;
}

int inserir_no_inicio_da_lista (Lista* li, Aluno al){
  if (li == NULL){
      return 0;
  }
  Elem* no;

  no = (Elem*) malloc(sizeof(Elem));
  if (no == NULL){
      return 0;
  }
  no -> dadosAlunos = al;
  no -> prox = li->primeiro;
  li->primeiro = no;
  return 1;
}

void mostra_lista(Lista list) {
    Elem *aux = list.primeiro;

    while (aux != NULL) {
        printf("\n %d - %s", aux->dadosAlunos.matricula, aux->dadosAlunos.nome);
        aux = aux->prox;
    }
}

int main() {
    Lista list; 
    Aluno a;

    criar_lista(&list);
    a.matricula = 1;
    strcpy(a.nome, "Jorge");
    a.n1 = 10;
    a.n2 = 8;
    a.n3 = 9;
    inserir_no_inicio_da_lista(&list, a);
    mostra_lista(list);
    return 0;
}
  • Did you even test it? It worked for you?

Browser other questions tagged

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