有关循环链表
脑子有点笨,老师让弄一个从一链表分成三种字符的循环链表,本以为可以跑通,结果,活活弄死机了两次,求大神,#include <stdio.h>
#include <stdlib.h>
#define ok 1
#define error 0
#define SIZE 100
typedef charElemType;
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;
p->next = q->next;
p->next = q;
p = q;
}
p->next = NULL;
return ok;
}
StatusResolvePList(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;
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);
} 已知由一个线性链表表示的线性表中含有三类字符(数字,字母,其他),试编写算法将该线性链表分割为三个循环链表,其中每个循环链表表示的线性表均只含一类字符。 学习,继续升级中 整个程序看下来有些乱,单看下面两个函数定义
Status LnitPList(PLinkList &L) //参数PLinkList &L表示什么?这里应该是初始化循环链表吧
//那形参应设为PLinkList *L型
{
L = (PLinkList)malloc(sizeof(PNode)); //按参数类型,这里用*L,下面也类似调整
if ( L == NULL )return error;
L->next = L;
return ok;
}
Status CreatePList(PLinkList &L,char a[],int n) //创建以字符串a为数据项的单链表?
//同样改为PLinkList *L
{
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;
p->next = q->next;
p->next = q;
p = q;
}
p->next = NULL;
return ok;
}
页:
[1]