供参考:
#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;
}