主题:[讨论]!!求站内高手帮忙找出这个程序中的错误关于循环链表的多项式分解!!!
/*这个程序是为了首先利用循环链表储存一个一元多项式,并把这个一元多项式分离成奇数多项式和偶数多项式,要求利用原链表空间,我编写了下面的算法,但是
并不能的出想要的结果,实在找不出错误了,所以请大家帮帮忙找出这个程序中的错误*/
#include<stdio.h>
#include<malloc.h>
typedef struct Polynode
{
int coef;
int exp;
struct Polynode *next;
}Polynode,*Polylist;
Polylist InitPoly(Polylist *L)
{
*L=(Polynode *)malloc(sizeof(Polynode));
(*L)->next=*L;
return *L;
} /*初始化循环链表*/
Polylist InsPoly(Polylist L,int c,int e)
{
Polynode *p,*s;
p=L;
while(p->next!=L)
p=p->next;
s=(Polynode *)malloc(sizeof(Polynode));
s->coef=c;
s->exp=e;
s->next=L;
p->next=s;
return L;
} /*循环链表元素的插入*/
Polylist DeletePoly(Polylist L,int *c,int *e)
{
Polynode *r;
r=L->next;
L->next=r->next;
*c=r->coef;
*e=r->exp;
free(r);
return L;
} /*循环链表元素的删除*/
void PrintPoly(Polylist L)
{
Polynode *p;
p=L->next;
if(p==L)printf("多项式无值\n");
else
{
printf("%dx^%d",p->coef,p->exp);
p=p->next;
while(p!=L)
{
printf("+%dx^%d",p->coef,p->exp);
p=p->next;
}
}
} /*链表元素的输出*/
void main()
{
int c,e,n=1;
Polynode *p;
Polylist A,Odd,Eve;
InitPoly(&A);
printf("请输入多项式链表元素(多项式指数递增输入并以0,0结束):");
printf("第%d项系数:",n);
scanf("%d",&c);
printf("指数:");
scanf("%d",&e);
n++;
while(c||e) /*当系数和指数有一项不为0那么循环while语句*/
{
InsPoly(A,c,e);
printf("第%d项系数:",n);
scanf("%d",&c);
printf("指数:");
scanf("%d",&e);
n++;
}
printf("原始多项式是:");
PrintPoly(A);
Odd=A;
Odd->next=Odd;
Eve=A;
Eve->next=Eve; /*利用原链表空间创建并初始化2个新链表储存奇数多项式和偶数多项式*/
p=A->next;
while(p!=A) /*如果p不等于头指针执行while语句*/
{
if((p->exp)%2==1)
{
DeletePoly(A,&c,&e);
InsPoly(Odd,c,e); /*如果p指向的结点的指数是奇数,删除此结点并把此结点插入奇数链表中*/
}
else if((p->exp)%2==0)
{
DeletePoly(A,&c,&e);
InsPoly(Eve,c,e); /*如果p指向的结点的指数是偶数,删除此结点并把此结点插入偶数链表中*/
}
p=p->next;
}
printf("\n奇数多项式是:");
PrintPoly(Odd);
printf("\n偶数多项式是:");
PrintPoly(Eve);
}
并不能的出想要的结果,实在找不出错误了,所以请大家帮帮忙找出这个程序中的错误*/
#include<stdio.h>
#include<malloc.h>
typedef struct Polynode
{
int coef;
int exp;
struct Polynode *next;
}Polynode,*Polylist;
Polylist InitPoly(Polylist *L)
{
*L=(Polynode *)malloc(sizeof(Polynode));
(*L)->next=*L;
return *L;
} /*初始化循环链表*/
Polylist InsPoly(Polylist L,int c,int e)
{
Polynode *p,*s;
p=L;
while(p->next!=L)
p=p->next;
s=(Polynode *)malloc(sizeof(Polynode));
s->coef=c;
s->exp=e;
s->next=L;
p->next=s;
return L;
} /*循环链表元素的插入*/
Polylist DeletePoly(Polylist L,int *c,int *e)
{
Polynode *r;
r=L->next;
L->next=r->next;
*c=r->coef;
*e=r->exp;
free(r);
return L;
} /*循环链表元素的删除*/
void PrintPoly(Polylist L)
{
Polynode *p;
p=L->next;
if(p==L)printf("多项式无值\n");
else
{
printf("%dx^%d",p->coef,p->exp);
p=p->next;
while(p!=L)
{
printf("+%dx^%d",p->coef,p->exp);
p=p->next;
}
}
} /*链表元素的输出*/
void main()
{
int c,e,n=1;
Polynode *p;
Polylist A,Odd,Eve;
InitPoly(&A);
printf("请输入多项式链表元素(多项式指数递增输入并以0,0结束):");
printf("第%d项系数:",n);
scanf("%d",&c);
printf("指数:");
scanf("%d",&e);
n++;
while(c||e) /*当系数和指数有一项不为0那么循环while语句*/
{
InsPoly(A,c,e);
printf("第%d项系数:",n);
scanf("%d",&c);
printf("指数:");
scanf("%d",&e);
n++;
}
printf("原始多项式是:");
PrintPoly(A);
Odd=A;
Odd->next=Odd;
Eve=A;
Eve->next=Eve; /*利用原链表空间创建并初始化2个新链表储存奇数多项式和偶数多项式*/
p=A->next;
while(p!=A) /*如果p不等于头指针执行while语句*/
{
if((p->exp)%2==1)
{
DeletePoly(A,&c,&e);
InsPoly(Odd,c,e); /*如果p指向的结点的指数是奇数,删除此结点并把此结点插入奇数链表中*/
}
else if((p->exp)%2==0)
{
DeletePoly(A,&c,&e);
InsPoly(Eve,c,e); /*如果p指向的结点的指数是偶数,删除此结点并把此结点插入偶数链表中*/
}
p=p->next;
}
printf("\n奇数多项式是:");
PrintPoly(Odd);
printf("\n偶数多项式是:");
PrintPoly(Eve);
}