Inserting elements into a dynamic list

1

I did this function, but I do not know if it's right, it does not give the compilation error, but it gives an error when prompt appears that the program stopped working.

Insertion at startup:

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

Create List:

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

  if (li != NULL){
      *li = NULL;
  }
  return li;
}
    
asked by anonymous 04.10.2017 / 14:37

2 answers

1

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

In addition, the logic is somewhat inconsistent. If the goal was to get NULL at startup, which is what usually makes the most sense, then just return NULL :

Lista* criar_lista() {
    return NULL;
}

Or assign NULL to main directly which was better.

int main(){
    Lista listaDeAlunos = NULL;
}

If the goal was to create a node 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 for a node. If it is sizeof(Lista) we are allocating space for a pointer that is not what we want.

But this creates other problems for you, as you end up creating an empty, empty data node and it will show the garbage you collect in memory.

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

Example working on Ideone with NULL on main

    
04.10.2017 / 15:30
0

Follow your code with some changes:

  • I created a struct for the list, having a pointer to the first Elem of the list
  • I modified a portion of your insert because it made wrong notes
  • I created a method to show the list

struct from 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;
}
    
04.10.2017 / 18:15