C语言日积月累(二)

使用c语言动态创建一个链表:

List_Dynamic_Create() {
    LinkList Head = (LinkList) malloc(sizeof(ListNode));
    Head->data = 0;
    Head->next = NULL;
    LinkList Tail = NULL;
    data_t value;
    while (1) {
        printf("input:");
        scanf("%d", &value);
        if (value == -1) {
            break;
        }
        LinkList NewNode = (LinkList) malloc(sizeof(ListNode));
        NewNode->data = value;
        NewNode->next = NULL;

        if(Head->next ==NULL){
            Head->next = NewNode;
            Tail = NewNode;
        }
        else{
            Tail->next = NewNode;
            Tail = NewNode;
        }
    }
    return Head;

}
滚动至顶部