Одна функция может сделать работу,

свободить узлы, выделенные в другой функции?

struct node {
    int data;
    struct node* next;
};

struct node* buildList()
{
    struct node* head = NULL;
    struct node* second = NULL;
    struct node* third = NULL;

    head = malloc(sizeof(struct node));
    second = malloc(sizeof(struct node));
    third = malloc(sizeof(struct node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    return head;
}  

Я вызываю функцию buildList в main ()

int main()
{
    struct node* h = buildList();
    printf("The second element is %d\n", h->next->data);
    return 0;
}  

Я хочу освободить голову, вторые и третьи переменные.
Благодарю.

Обновить:

int main()
{
    struct node* h = buildList();
    printf("The element is %d\n", h->next->data);  //prints 2
    //free(h->next->next);
    //free(h->next);
    free(h);

   // struct node* h1 = buildList();
    printf("The element is %d\n", h->next->data);  //print 2 ?? why?
    return 0;
}

Оба отпечатка 2. Не следует звонить бесплатно (h) удалить h. Если так, то почему доступны h-> next-> data, если h свободен? Конечно, «второй» узел не освобождается. Но поскольку голова удалена, она должна иметь возможность ссылаться на следующий элемент. В чем здесь ошибка?

Ответы на вопрос(4)

Ваш ответ на вопрос