2.33习题实例代码
#include<stdio.h>
#include<stdlib.h>
char sample[10] = {'1', 'a', ',', 't', '5', '@', '%', '.', ':'};
typedef struct LNode{
char data;
struct LNode *next;
}LNode, *LinkList;
void init(LinkList L){
int i;
LinkList q, temp;
q = L;
for(i=0; i<9; ++i){
temp = (LinkList)malloc(sizeof(LNode));
temp->data = sample[i];
q->next = temp;
q = temp;
}
q->next = NULL;
}
void LiskDivideInto3CL(LinkList L, LinkList s1, LinkList s2, LinkList s3)
{
LinkList p, q, pt1, pt2, pt3;
p = L->next;
pt1 = s1;
pt2 = s2;
pt3 = s3;
while(p){
if(p->data >= '0' && p->data<='9')
{
q = p;
p = p->next;
q->next = pt1->next;
pt1->next = q;
pt1 = pt1->next;
}
else if((p->data>='A' && p->data<='Z') || (p->data>='a' && p->data<='z'))
{
q = p;
p = p->next;
q->next = pt2->next;
pt2->next = q;
pt2 = pt2->next;
}
else{
q = p;
p = p->next;
q->next = pt3->next;
pt3->next = q;
pt3 = pt3->next;
}
q=L;
free(q);
}
}
int main(){
LNode L, s1, s2, s3;
L.next = NULL;
s1.next = &s1;
s2.next = &s2;
s3.next = &s3;
init(&L);
q = L.next;
while(q!=&s1)
{
printf("%c\n", q->data);
q=q->next;
}
LiskDivideInto3CL(&L, &s1, &s2, &s3);
LinkList q;
q = s1.next;
while(q!=&s1)
{
printf("%c\n", q->data);
q=q->next;
}
q = s2.next;
while(q!=&s2)
{
printf("%c\n", q->data);
q=q->next;
}
q = s3.next;
while(q!=&s3)
{
printf("%c\n", q->data);
q=q->next;
}
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-36266.html