Chained List C does not insert new node

Asked

Viewed 119 times

0

I started making a chained list program, but when I use the print function, it wasn’t printing anything, so I discovered that the LISTA after leaving the function inserts, it again has the value NULL and I don’t 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;
      }
    }
  • The problem is the return of the same pointer. Then I see if there are other errors, I need to leave quickly.

  • In function insere has a while that seems suspicious to me. You start walking through the list and insert the item right in the first iteration. I think the correct one would be to move that p2->prox = p1; for after the while.

  • @jHertel Well noted, but only optimized the code, does not solve the problem :(

  • @Brunocasas Any of the answers solved your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

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 though it is a pointer you need to pass its address so that it is reflected in what you move within the function. Then you have to call with &.

You also have the option to return the modified list in the function. I will show only the first one. You have an example of the second in I am unable 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);
}

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

  • Thank you very much man, solved the problem :D

  • @Brunocasas take a look at [tour] the best way to say thank you.

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; every iteration in your while, and must return a pointer to pointer as the function is void.

  • Thank you so much for your help

Browser other questions tagged

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