Chained List C does not insert new node

0

I started to do a linked list program, but when I use the print function, it was not printing anything, so I discovered that LISTA after exiting the function inserts it, it returns the value NULL and I I do not know why.

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

    struct Node {
     int num;
     struct Node *prox;
    };
    typedef struct Node node;

    node* aloca();
    void inicia(node *LISTA);
    void insere(node *LISTA, int val);
    void imprime(node *LISTA);

    int main(void) {
      node *LISTA = NULL;
      inicia(LISTA);
      insere(LISTA, 10);
      insere(LISTA, 20);
      insere(LISTA, 5);
      imprime(LISTA);

      return 0;
    }

    node* aloca() {
      node *LISTA = (node *)malloc(sizeof(node));
      return(LISTA);
    }

    void inicia(node *LISTA) {
      LISTA = NULL;
    }

    void insere(node *LISTA, int val) {
      node *p1 = aloca();
      node *p2;

      p1->num = val;
      p1->prox = NULL;

      if (LISTA == NULL) {
        LISTA = p1;
      }else {
        p2 = LISTA;
        while (p2->prox != NULL) {
          p2 = p2->prox;
          p2->prox = p1;
        }
      }
    }

    void imprime(node *LISTA) {
      node *tmp;
      tmp = LISTA;
      while(tmp != NULL) {
        printf("\nasfdsdf");
        printf("%d", tmp->num);
        tmp = tmp->prox;
      }
    }
    
asked by anonymous 28.02.2017 / 14:48

2 answers

2

The code can be greatly improved and simplified. But the main problem is that you are not passing the list by reference. Even if it is a pointer, you must pass the address so that it is reflected in the pointer. Then you have to call with & .

You also have the option to return the modified list in the function. I'll show you the first one. It has an example of the second one in I am not able to pass one vector per parameter in C

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

typedef struct Node {
    int num;
    struct Node *prox;
} Node;

void imprime(Node *lista) {
    Node *tmp = lista;
    while (tmp != NULL) {
        printf("%d\n", tmp->num);
        tmp = tmp->prox;
    }
}

void insere(Node **lista, int val) {
    Node *node = malloc(sizeof(Node));
    node->num = val;
    node->prox = NULL;
    if (*lista == NULL) {
        *lista = node;
    } else {
        Node *atual = *lista;
        while (atual->prox != NULL) {
            atual = atual->prox;
        }
        atual->prox = node;
    }
}

int main(void) {
    Node *lista = NULL;
    insere(&lista, 10);
    insere(&lista, 20);
    insere(&lista, 5);
    imprime(lista);
}

See running on ideone . And No Coding Ground . Also put it on GitHub for future reference .

    
28.02.2017 / 16:20
0

Change the code to:

 void insere(node **LISTA, int val) {
      node *p1 = aloca();
      node *p2;

      p1->num = val;
      p1->prox = NULL;

      if (*LISTA == NULL) {
        *LISTA = p1;
      }else {
        p2 = *LISTA;
        while (p2->prox != NULL) {
          p2 = p2->prox;
        }
         p2->prox = p1;
      }
    }

You were using p2->prox = p1; for each iteration in your while, and you should return a pointer to pointer since the function is void.

    
28.02.2017 / 15:42