多项式相加的函数(数据结构_C语言版)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
s->exp=pa->exp;
s->coef=pa->coef;
tc->next=s;
tc=s;
pa=pa->next;
}
else if(pa->exp<pb->exp)
{s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pb->exp;
s->coef=pb->coef;
tc->next=s;
tc=s;
pa=pa->next;
}
tc->next=NULL;
}
void main()
{
PolyNode *ha,*hb,*hc;
PolyArray a={{1.5,0},{2.5,1},{3.3,3},{-2.5,5}};
PolyArray b={{-1.5,0},{2.5,1},{3.7,3},{-2.5,5},{5.6,7}};
{float coef;//系数
int exp;//指数
struct pnode *next;
}PolyNode;
void DispPoly(PolyNode *L)//输出多项式
{PolyNode *p=L->next;
while(p!=NULL)
{printf("%gx^%d",p->coef,p->exp);
L->next=NULL;
r=L;//r始终指向终端结点,开始时指向头结点
for (i=0;i<n;i++)
{
s=(PolyNode *)malloc(sizeof(PolyNode));//创建新结点
s->coef=a[i].coef;
s->exp=a[i].exp;
r->next=s;//将*s插入*r之后
CreateListR(ha,a,4);
CreateListR(hb,b,5);ห้องสมุดไป่ตู้
printf("原多项式A为:");DispPoly(ha);
printf("原多项式B为:");DispPoly(hb);
Sort(ha);
Sort(hb);
printf("有序多项式A:");DispPoly(ha);
tc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL)
pa=pb;//复制余下的结点
while(pa!=NULL)
{s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pa->exp;
s->coef=pa->coef;
r=s;
}
r->next=NULL;
}
void Sort(PolyNode *&head)//按exp域递减排序
{PolyNode *p=head->next,*q,*r;
if(p!=NULL)
{r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{r=p->next;
tc->next=s;
tc=s;
pb=pb->next;
}
else
{c=pa->coef+pb->coef;
if(c!=0)//系数之和不为0事创建新结点
{s=(PolyNode *)malloc(sizeof(PolyNode));
s->exp=pa->exp;
s->coef=c;
tc->next=s;
p=p->next;
}
printf("\n");
}
void CreateListR(PolyNode *&L,PolyArray a,int n)
//尾插法建立单链表
{PolyNode *s,*r;int i;
L=(PolyNode *)malloc(sizeof(PolyNode));//创建头结点
q=head;
while(q->next!=NULL&&q->next->exp>p->exp)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}
void Add(PolyNode *ha,PolyNode *hb,PolyNode *&hc)//求两个有序集合的并
printf("有序多项式B:");DispPoly(hb);
Add(ha,ha,hc);
printf("多项式相加结果:");DispPoly(hc);printf("\n");
}
{
PolyNode *pa=ha->next,*pb=hb->next,*s,*tc;
float c;
hc=(PolyNode *)malloc(sizeof(PolyNode));
tc=hc;
while(pa!=NULL&&pb!=NULL)
{if(pa->exp>pb->exp)
{s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
一、编写一个程序用单链表存储多项式,并实现两个多项式相加的函数。
【源程序】#include<stdio.h>
#include <malloc.h>
#define MAX 20
typedef struct
{float coef;
int exp;
}PolyArray[MAX];
typedef struct pnode
s->coef=pa->coef;
tc->next=s;
tc=s;
pa=pa->next;
}
else if(pa->exp<pb->exp)
{s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pb->exp;
s->coef=pb->coef;
tc->next=s;
tc=s;
pa=pa->next;
}
tc->next=NULL;
}
void main()
{
PolyNode *ha,*hb,*hc;
PolyArray a={{1.5,0},{2.5,1},{3.3,3},{-2.5,5}};
PolyArray b={{-1.5,0},{2.5,1},{3.7,3},{-2.5,5},{5.6,7}};
{float coef;//系数
int exp;//指数
struct pnode *next;
}PolyNode;
void DispPoly(PolyNode *L)//输出多项式
{PolyNode *p=L->next;
while(p!=NULL)
{printf("%gx^%d",p->coef,p->exp);
L->next=NULL;
r=L;//r始终指向终端结点,开始时指向头结点
for (i=0;i<n;i++)
{
s=(PolyNode *)malloc(sizeof(PolyNode));//创建新结点
s->coef=a[i].coef;
s->exp=a[i].exp;
r->next=s;//将*s插入*r之后
CreateListR(ha,a,4);
CreateListR(hb,b,5);ห้องสมุดไป่ตู้
printf("原多项式A为:");DispPoly(ha);
printf("原多项式B为:");DispPoly(hb);
Sort(ha);
Sort(hb);
printf("有序多项式A:");DispPoly(ha);
tc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL)
pa=pb;//复制余下的结点
while(pa!=NULL)
{s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
s->exp=pa->exp;
s->coef=pa->coef;
r=s;
}
r->next=NULL;
}
void Sort(PolyNode *&head)//按exp域递减排序
{PolyNode *p=head->next,*q,*r;
if(p!=NULL)
{r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{r=p->next;
tc->next=s;
tc=s;
pb=pb->next;
}
else
{c=pa->coef+pb->coef;
if(c!=0)//系数之和不为0事创建新结点
{s=(PolyNode *)malloc(sizeof(PolyNode));
s->exp=pa->exp;
s->coef=c;
tc->next=s;
p=p->next;
}
printf("\n");
}
void CreateListR(PolyNode *&L,PolyArray a,int n)
//尾插法建立单链表
{PolyNode *s,*r;int i;
L=(PolyNode *)malloc(sizeof(PolyNode));//创建头结点
q=head;
while(q->next!=NULL&&q->next->exp>p->exp)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}
void Add(PolyNode *ha,PolyNode *hb,PolyNode *&hc)//求两个有序集合的并
printf("有序多项式B:");DispPoly(hb);
Add(ha,ha,hc);
printf("多项式相加结果:");DispPoly(hc);printf("\n");
}
{
PolyNode *pa=ha->next,*pb=hb->next,*s,*tc;
float c;
hc=(PolyNode *)malloc(sizeof(PolyNode));
tc=hc;
while(pa!=NULL&&pb!=NULL)
{if(pa->exp>pb->exp)
{s=(PolyNode *)malloc(sizeof(PolyNode)); //复制结点
一、编写一个程序用单链表存储多项式,并实现两个多项式相加的函数。
【源程序】#include<stdio.h>
#include <malloc.h>
#define MAX 20
typedef struct
{float coef;
int exp;
}PolyArray[MAX];
typedef struct pnode