一个关于C语言链表的问题

已知有一个乱序的字符序列L,序列中的字符可能是英文字母、数字字符或其它字符,字符的个数未知,每个字符之间用空格分开。字符序列用“-1”作为输入结束标志,这里你要把-1当做一个字符串对待,并且不算作字符序列中的元素。如下即为一个合法的字符序列:“a c 3 b a d 6 , & j m 8 7 2 V -1”。你的任务是将这个字符序列拆分为三个独立的序列A、B和C,其中序列A存放序列L中的字母,序列B存放序列L中的数字,序列C存放序列L中的其他字符,然后,将序列A、B和C分别按照ASCII码的大小关系进行升序排序。最终序列L将变为空序列。
要求:
建立四个单链表,分别存储序列L、A、B、C中的元素。字符序列的输入用“-1”作为结束标志。建立链表L时,建议使用scanf(“%s”,s);来读取字符序列中的字符,即把单独的字符看做一个字符串读取。当L建立后,你要按照问题描述中所述,将L拆分为A、B、C三个链表,然后对每个链表都进行排序,这部分的操作都应该是对指针进行修改,而不是删除节点与建立新节点。在程序结束前要释放链表A、B、C中的所有节点。

下面是样例:
1.
输入:
a c 3 b a d 6 , & j m 8 7 2 V -1
输出:
The list A is: V a a b c d j m
The list B is: 2 3 6 7 8
The list C is: & ,
2.
输入:
z m v 1 a K 2 m p 9 a 0 a d -1
输出:
The list A is: K a a a d m m p v z
The list B is: 0 1 2 9
There is no item in C list.

供参考:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct node {
    char    data;
    struct node* next;
}Node,*list;
void createlist(list* L)
{
    char str[3];
    Node* p = NULL, * pL = NULL;
    while (1)
    {
        scanf("%s", str);
        if (!strcmp(str, "-1"))  break;
        p = (list)malloc(sizeof(Node));
        p->next = NULL;
        p->data = str[0];
        if (!(*L))
            (*L) = p;
        else
            pL->next = p;
        pL = p;
    }
}

void printlist(list L)
{
    Node* p = NULL;
    if (!L)
        printf("NULL");
    else {
        p = L;
        while (p) {
            printf(p == L ? "%c" : " %c", p->data);
            p = p->next;
        }
    }
    printf("\n");
}

list insert(list L, Node* pt)
{
    list p = L;
    while (p->next && p->next->data < pt->data)
    {
        p = p->next;
    }
    if (p == L && p->data > pt->data) {
        pt->next = L;
        L = pt;
    }
    else {
        pt->next = p->next;
        p->next = pt;
    }
    return L;
}

void split_sort(list* L, list* A, list* B, list* C) //拆分 + 排序
{
    Node* pL = (*L), * pt = NULL, * pA = NULL, * pB = NULL, * pC = NULL;
    (*L) = NULL;
    while (pL)
    {
        pt = pL;
        pL = pL->next;
        pt->next = NULL;
        if (isalpha(pt->data)) {    //字母链入A
            if (!(*A))
                (*A) = pt;
            else 
                (*A) = insert((*A), pt);
        }
        else if (isalnum(pt->data)) { //数字链入B
            if (!(*B))
                (*B) = pt;
            else
                (*B) = insert((*B), pt);
        }
        else {                     //其他字符链入C
            if (!(*C))
                (*C) = pt;
            else
                (*C) = insert((*C), pt);
        }
    }
}

void destroy(list L)
{
    Node* pt = NULL;
    while (L)
    {
        pt == L;
        L = L->next;
        free(pt);
    }
    L = NULL;
}

int main()
{
    list L = NULL, A = NULL, B = NULL, C = NULL;

    createlist(&L);

    split_sort(&L, &A, &B, &C);

    if (A) {
        printf("The list A is:");
        printlist(A);
    }
    else
        printf("There is no item in A list.\n");

    if (B) {
        printf("The list B is:");
        printlist(B);
    }
    else
        printf("There is no item in B list.\n");

    if (C) {
        printf("The list C is:");
        printlist(C);
    }
    else
        printf("There is no item in C list.");

    destroy(A);
    destroy(B);
    destroy(C);
    return 0;
}

该回答引用chatgpt:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LENGTH 1000

typedef struct Node {
    char value[MAX_LENGTH];
    struct Node* next;
} Node;

void print_list(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%s ", current->value);
        current = current->next;
    }
    printf("\n");
}

void free_list(Node* head) {
    Node* current = head;
    while (current != NULL) {
        Node* next = current->next;
        free(current);
        current = next;
    }
}

int is_letter(char c) {
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

int is_digit(char c) {
    return c >= '0' && c <= '9';
}

int is_other(char c) {
    return !(is_letter(c) || is_digit(c));
}

void split_list(Node* l, Node** a, Node** b, Node** c) {
    Node* current = l;
    while (current != NULL) {
        Node* next = current->next;
        if (is_letter(current->value[0])) {
            current->next = *a;
            *a = current;
        } else if (is_digit(current->value[0])) {
            current->next = *b;
            *b = current;
        } else if (is_other(current->value[0])) {
            current->next = *c;
            *c = current;
        }
        current = next;
    }
}

void sort_list(Node** head) {
    Node** i, **j;
    for (i = head; *i != NULL; i = &(*i)->next) {
        for (j = &(*i)->next; *j != NULL; j = &(*j)->next) {
            if (strcmp((*i)->value, (*j)->value) > 0) {
                Node* temp = *i;
                *i = *j;
                *j = temp;
            }
        }
    }
}

int main() {
    Node* head = NULL;
    Node* current = NULL;
    while (1) {
        char value[MAX_LENGTH];
        scanf("%s", value);
        if (strcmp(value, "-1") == 0) {
            break;
        }
        Node* node = (Node*)malloc(sizeof(Node));
        strcpy(node->value, value);
        node->next = NULL;
        if (head == NULL) {
            head = node;
        } else {
            current->next = node;
        }
        current = node;
    }

    Node* a = NULL;
    Node* b = NULL;
    Node* c = NULL;

    split_list(head, &a, &b, &c);

    sort_list(&a);
    sort_list(&b);
    sort_list(&c);

    printf("The list A is: ");
    print_list(a);
    printf("The list B is: ");
    print_list(b);
    if (c != NULL) {
        printf("The list C is: ");
        print_list(c);
    } else {
        printf("There is no item in C list.\n");
    }

    free_list(head);
    free_list(a);
    free_list(b);
    free_list(c);

    return 0;
}