|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
脑子有点笨,老师让弄一个从一链表分成三种字符的循环链表,本以为可以跑通,结果,活活弄死机了两次,求大神,
#include <stdio.h>
#include <stdlib.h>
#define ok 1
#define error 0
#define SIZE 100
typedef char ElemType;
typedef int Status;
typedef struct PNode
{
ElemType data;
struct PNode *next;
}PNode,*PLinkList;
Status LnitPList(PLinkList &L)
{
L = (PLinkList)malloc(sizeof(PNode));
if ( L == NULL )return error;
L->next = L;
return ok;
}
Status CreatePList(PLinkList &L,char a[],int n)
{
PLinkList p,q;
int i;
if( L )return error;
p = L;
for( i = 0;i < SIZE;i++ )
{
q = (PLinkList)malloc(sizeof(PNode));
q->data = a[i];
p->next = q->next;
p->next = q;
p = q;
}
p->next = NULL;
return ok;
}
Status ResolvePList(PLinkList L,PLinkList &ha,PLinkList &hb,PLinkList &hc)
{
PLinkList p,p1,p2,p3,q1,q2,q3,s1,s2,s3;
p = L;
if (!L)return error;
while(L&&p->data!='\n')
{
if ( p->data >= 0&&p->data <= 9 )
{
p1 = ha;
q1 = (PLinkList)malloc(sizeof(PNode));
q1->data = p->data;
q1->next = p1->next;
p1->next =q1;
p1 = q1;
}
else if( p->data >= 'a'&&p->data <= 'z'||p->data >= 'A'&&p->data<='Z')
{
p2 = hb;
q2 = (PLinkList)malloc(sizeof(PNode));
q2->data = p->data;
q2->next = p2->next;
p2->next =q2;
p2 = q2;
}
else
{
p3 = hb;
q3 = (PLinkList)malloc(sizeof(PNode));
q3->data = p->data;
q3->next = p3->next;
p3->next =q3;
p3 = q3;
}
}
p1->next = ha->next;
s1 = ha;
ha = s1->next;
free(s1);
p2->next = hb->next;
s2 = hb;
hb = s2->next;
free(s2);
p3->next = hc->next;
s3 = hc;
hc = s3->next;
free(s3);
return ok;
}
void print(PLinkList &L)
{
PLinkList p;
p = L;
do
{
printf("%c",p->data);
p = p->next;
}
while(p->data != L->data);
}
void main()
{
char datas[SIZE];
PNode *H,*H1,*H2,*H3;
printf("输入一段字符串!\n");
scanf("%s",datas);
LnitPList(H);
LnitPList(H1);
LnitPList(H2);
LnitPList(H3);
CreatePList(H,datas,SIZE);
ResolvePList(H,H1,H2,H3);
print(H1);
print(H2);
print(H3);
} |
|