用链表实现一元稀疏多项式的相加

来源:互联网 发布:sql int转date 编辑:程序博客网 时间:2024/05/17 05:09

        由于一元多项式包含系数项和指数项,其系数为float型,指数项为int型,往往将其两项组合成为一个结构元素类型,将多项式看成是一个有序表,则多项式定义中的各个操作均可利用有序表操作来完成。

       “建立多项式算法”操作结果:输入m项的系数和指数,建立一元多项式P;

       “销毁多项式算法”初始条件:一元多项式p已存在;

                                 操作结果:销毁一元多项式

        “输出多项式算法”初始条件:一元多项式p已存在;

                                   操作结果:打印一元多项式p;

       “多项式加法算法”初始条件:两个多项式的Pa,Pb已存在;

                                 操作结果:pa=pa+pb,并销毁pb;

# include<stdio.h># include<malloc.h>typedef struct{float coef;  //系数int expn;    //指数}ElemType;typedef struct LNode{  //结点类型ElemType data;struct LNode *next;}*LinkList;void MakeNode(LinkList &s,ElemType e){//生成结点s=(LinkList)malloc(sizeof(LNode));s->data=e;}void InsAfter(LinkList p,LinkList s){//插入结点s->next=p->next;p->next=s;}int compare(ElemType e1,ElemType e2){//比较if(e1.expn>e2.expn)return 1;else if(e1.expn<e2.expn)return -1;return 0;}void OrderInsert(LinkList &L,ElemType e,int(*compare)(ElemType,ElemType)){//有序插入LinkList p=L,q=p->next,s;while(q){int n=compare(e,q->data);if(n<0){  MakeNode(s,e);InsAfter(p,s);return;}else if(n==0)  break;p=p->next;q=p->next;}MakeNode(s,e);InsAfter(p,s); //最大,放在最后一个位置}void InitList(LinkList &L){//初始化L=(LinkList)malloc(sizeof(LNode));L->next=NULL;}void SetCurElem(LinkList &p,ElemType e){ //设置结点p->data.coef=e.coef;p->data.expn=e.expn;}void CreatePolyn(LinkList &L,int m){InitList(L);ElemType e;e.coef=0.0;e.expn=-1;SetCurElem(L,e);//设置头结点的数据元素for(int i=1;i<=m;i++){scanf("%f%d",&e.coef,&e.expn);  //输入值OrderInsert(L,e,compare);}}void show(LinkList L){//输出方法LinkList p=L->next;if(p){ //第一个输出printf("%f^%d",p->data.coef,p->data.expn);p=p->next;}while(p){if(p->data.coef>0)printf("+");printf("%f^%d",p->data.coef,p->data.expn);p=p->next;}printf("/n");}void ListDestroy(LinkList &L){//销毁LinkList p=L,q;while(p){q=p;p=p->next;free(q);}}void Add(LinkList &L1,LinkList &L2){//多项式相加LinkList p1=L1->next,p2=L2->next,r1=L1,q;//r1始终指向链表的尾部,p1和p2表示当前结点while(p1&&p2){int n=compare(p1->data,p2->data);switch(n){case -1:{r1->next=p1;r1=p1;p1=p1->next;break;} //L1中的值小,插入之case 1:{r1->next=p2;r1=p2;p2=p2->next;break;}   //L2中的值小,插入之case 0:{    //相同    float sum=p1->data.coef+p2->data.coef;if(sum==0){q=p1;p1=p1->next;free(q);} //删除L1中的    else{p1->data.coef=sum;  //插入L1中的         r1->next=p1;r1=p1;p1=p1->next;}q=p2;p2=p2->next;free(q);  //统一删除L2中的   }}}if(p1)r1->next=p1;  //添加L1elser1->next=p2;  //添加L2free(L2);L2=q=NULL;p1=p2=r1=NULL;}


# include"cf.h"void main(){LinkList L1,L2;CreatePolyn(L1,4);show(L1);        CreatePolyn(L2,3);show(L2);Add(L1,L2);show(L1);}

输入一元多项式L1(4),L2(3)

输出L1+L2


0 0
原创粉丝点击