Odd problema com o ponteiro ao implementar uma lista vinculada
Estou tentando implementar uma lista vinculada em C e quero armazenar o nó principal em uma estrutura separada. No entanto, parece que o nó principal está sendo reatribuído de alguma forma sempre que adiciono outro n
#include <stdio.h>
#include <stdlib.h>
struct BC_node {
struct BC_node *next;
void *data;
};
struct BC_list {
struct BC_node *head;
struct BC_node *tail;
};
void
BC_list_push(struct BC_list *list, void *data)
{
struct BC_node *node = calloc(1, sizeof(struct BC_node));
if (list->head != NULL)
printf("head: %d\n", *((int *) (list->head)->data));
node->next = NULL;
node->data = data;
if (list->head == NULL) {
printf("head is null.\n");
list->head = node;
}
if (list->tail != NULL) {
(list->tail)->next = node;
}
list->tail = node;
printf("head: %d\n", *((int *) (list->head)->data));
}
int
main(void)
{
int i;
struct BC_list *list = calloc(1, sizeof(struct BC_list));
list->head = NULL;
list->tail = NULL;
for (i = 0; i < 3; i++)
BC_list_push(list, &i);
return 0;
}
A saída
head is null.
head: 0
head: 1
head: 1
head: 2
head: 2