#include <stdio.h>

#include <stdlib.h>

#include <string.h>


typedef struct {

    char name[100];

}element;


typedef struct ListNode {

    element data;

    struct ListNode* link;

}ListNode;


void error(char* message)

{

    fprintf(stderr, "%s\n", message);

    exit(1);

}


ListNode* insert_first(ListNode* head, element value)

{

    ListNode* p = (ListNode*)malloc(sizeof(ListNode));

    p->data = value;

    p->link = head;

    head = p;

    return head;

}


ListNode* delete_first(ListNode* head)

{

    ListNode* removed;

    if (head == NULL) return NULL;

    removed = head->link;

    head->link = removed->link;

    free(removed);

    return head;

}


ListNode* delete(ListNode* head, ListNode* pre)

{

    ListNode* removed;

    removed = pre->link;

    pre->link = removed->link;

    free(removed);

    return head;

}


ListNode* reverse(ListNode* head)

{

    ListNode *p, *q, *r;

    

    p = head;

    q = NULL;

    while (p != NULL) {

        r = q;


        q = p;

        p = p->link;

        q->link = r;

    }

    return q;

}


ListNode* concat_list(ListNode* head1, ListNode* head2)

{

    if (head1 == NULL) return head2;

    else if (head2 == NULL) return head1;

    else {

        ListNode* p;

        p = head1;

        while (p->link != NULL)

        {

            p = p->link;

        }

        p->link = head2;

        return head1;

    }

}


void print_list(ListNode* head)

{

    for (ListNode* p = head; p != NULL; p = p->link)

        printf("%s->", p->data.name);

    printf("NULL \n");

}


int main(void)

{

    ListNode* head1 = NULL;

    ListNode* head2 = NULL;

    element data;



    strcpy_s(data.name, (unsigned int)sizeof(data.name), "Grapes");

    head1 = insert_first(head1, data);

    strcpy_s(data.name, (unsigned int)sizeof(data.name), "Apple");

    head1 = insert_first(head1, data);

    strcpy_s(data.name, (unsigned int)sizeof(data.name), "Strawberry");

    head1 = insert_first(head1, data);

    strcpy_s(data.name, (unsigned int)sizeof(data.name), "Mandarin");

    head1 = insert_first(head1, data);

    strcpy_s(data.name, (unsigned int)sizeof(data.name), "Mango");

    head1 = insert_first(head1, data);


    printf("List(데이터): ");

    print_list(head1);


    head1 = reverse(head1);

    printf("List(역순데이터): ");

    print_list(head1);


    strcpy_s(data.name, (unsigned int)sizeof(data.name), "Banana");

    head2 = insert_first(head2, data);

    strcpy_s(data.name, (unsigned int)sizeof(data.name), "Melon");

    head2 = insert_first(head2, data);

    strcpy_s(data.name, (unsigned int)sizeof(data.name), "Lemon");

    head2 = insert_first(head2, data);

    printf("List2(데이터): ");

    print_list(head2);


    head1 = concat_list(head1, head2);

    printf("List(병합 후): ");

    print_list(head1);




    head1 = delete(head1, head1->link);

    printf("List(데이터2 삭제후): ");

    print_list(head1);


    head1 = delete(head1, head1->link->link->link->link->link);

    printf("List(데이터 마지막 삭제후): ");

    print_list(head1);


    delete_first(head1);

    printf("List(데이터): ");

    print_list(head1);


}


일단 현재 작성한 전문인데 delete_first 함수를


ListNode* delete_first(ListNode* head)

{

    ListNode* removed;

    if (head == NULL) return NULL;

    removed = head->link;

    head->link = removed->link;

    free(removed);

    return head;

}


사용하면 첫 노드를 지워주는데 하나가 남았을 때 지워지지 않고


delete_first(head2);

delete_first(head2);

delete_first(head2);

print_list(head2);


를 작성하면, 결과가 


NULL이 아니라


Banana->NULL 이 출력 되는데


리스트를 정수로 입력하니까 또 괜찮고, 혹시 왜 그런지 알 수 있을까요?