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;
}
}