/*
已知一个带头结点的单链表lc中结点数据元素类型为
字符型数据,主在包括二类字符(字母字符与数字字符),
试设计一个算法,将lc拆分成二个链表la与lb,
其中la的数据为字母字符,lb的数据为数字字符。
*/
# include <iostream>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
using namespace std;
typedef struct Node
{
char elem;
struct Node* next;
}*linklist;
void inlinklist(linklist &l, int n)
{
l = (linklist) malloc(sizeof(Node));
l->next = NULL;
linklist p, end;
end = l;
printf("请输入链表元素:\n");
for(int i = 0; i<n; i++)
{
p = (linklist) malloc (sizeof(Node));
scanf("%c", &p->elem);
end->next = p;
p->next = NULL;
end = p;
}
}
void outlinklist(linklist l)
{
printf("链表中的元素为:\n");
linklist p;
p = l->next;
while(p)
{
printf("%c ", p->elem);
p = p->next;
}
printf("\n");
}
void splitlinklist(linklist &l1, linklist &l2, linklist &l3)
{
l2 = (linklist) malloc (sizeof(Node));
l3 = (linklist) malloc (sizeof(Node));
l2->next = NULL;
l3->next = NULL;
linklist q, p, s;
p = l1->next;
q = l2;
s = l3;
while(p)
{
if(((p->elem) >= 'a' && (p->elem) <= 'z') || ((p->elem) >= 'A' && (p->elem) <= 'Z'))
{
q->next = p;
q = q->next;
p = p->next;
}
else if((p->elem) >= '0' && (p->elem) <= '9')
{
s->next = p;
p = p->next;
s = s->next;
}
}
q->next = NULL;
s->next = NULL;
}
int main()
{
linklist l1, l2, l3;
int n;
printf("请输入链表的元素个数:\n");
scanf("%d", &n);
getchar();
inlinklist(l1 ,n);
splitlinklist(l1, l2, l3);
outlinklist(l2);
outlinklist(l3);
return 0;
}