建立一个工程,包括三个文件。其中LinkList.h是头文件,LinkList.cpp 是具体函数实现的文件,2multiplePolyn.cpp是主函数所在的文件
头文件:
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
typedef struct LNode//节点类型
{
float coef;//系数
int expn;//指数
struct LNode next;//指针域
}Node, polynmail;
#define LEN sizeof(struct LNode)
polynmail Attach(float c,int e,polynmail pRear);//往多项式链表中添加节点
polynmail CreatePolyn(int m);//输入m项的系数和指数,建立表示一元多项式的有序链表P
polynmail MultiplePolyn(polynmail P1,polynmail P2);// pA=pApB,销毁pB
polynmail AddPolyn(polynmail P1,polynmail P2);//多项式相加
int cmp(int a, int b);
void PrintPolyn(polynmail P);
#endif // LINKLIST_H_INCLUDED
具体子函数实现的文件:
#include
#include"LinkList.h"
#include<malloc.h>
using namespace std;
int cmp(int a,int b)
{
if(a<b)
return -1;
else if(a==b)
return 0;
else return 1;
}
polynmail CreatePolyn(int m)
{
polynmail head,p1,p2;
int n=1;
p1=p2=(Node)malloc(LEN);
p1->next=NULL;
p2->next=NULL;
while(n<=m)
{
if(n1)
{
cin>>p1->coef>>p1->expn;
head=p1;
}
else
{
p2=p1;
p1=(Node*)malloc(LEN);
cin>>p1->coef>>p1->expn;
p2->next=p1;
}
n++;
}
p1->next=NULL;
return head;
}
polynmail Attach(float c,int e,polynmail pRear)//插入节点
{
float sum=0;
polynmail p,pb,head;
head=pRear;//头结点是head,我们操作pRear
p=(Node*)malloc(LEN);
p->coef=c;
p->expn=e;
p->next=NULL;
if(headNULL)//头结点为空,直接插入
{
head=p;
p->next=NULL;
return head;
}
while((p->expn > pRear ->expn) && (pRear->next!=NULL))//用pRear遍历多项式
{
//pf始终指向当前节点,而pb指向上一个节点
pb=pRear;
pRear=pRear->next;
}
if(pRear->expnp->expn)//合并,考虑尾节点
{
sum=pRear->coef+p->coef;
if(sum!=0)
{
pRear->coef=sum;
}
else if(sum0)
{
if(pRear->nextNULL){//删除尾节点
pb->next=NULL;
free(pRear);
}
else if(pRearhead)//删除头结点
{
pb=head;
head=head->next;
free(pb);
}
else if(pRear!=head)//删除非头结点
{
pb->next=pRear->next;
free(pRear);
}
}
}
else if(pRear->expn > p->expn) //插入
{
if(pRear==head )//找到的是头结点,插到头结点的前面
{
p->next=head;
head=p;
}
else //找到的是中间节点,故插到中间
{
p->next = pRear;
pb->next = p;
}
}
else //没有找到比新节点指数大的节点,故插在最后面
{
pRear->next=p;
p->next=NULL;
}
return head;
}
polynmail AddPolyn(polynmail P1,polynmail P2)//多项式相加
{
polynmail P;
float sum=0;
if(!P1 && !P2)
{
if(!P1) //如果A是空链表
return P2;
else
return P1;
}
P = (Node*)malloc(LEN);
P->coef=0;
P->expn=0;
P->next = NULL;
while(P1 && P2)
{
switch(cmp(P1->expn,P2->expn))
{
case -1://P1中当前节点的指数小
//把P1当前节点添加到Rear中去
P=Attach(P1->coef,P1->expn,P);
P1=P1->next;
break;
case 1://P2中当前节点的指数小
P=Attach(P2->coef,P2->expn,P);
P2=P2->next;
break;
case 0://指数一样大
sum=P1->coef+P2->coef;
if(sum!=0)
{
P=Attach(sum,P1->expn,P);
}
P1=P1->next;
P2=P2->next;
break;
}
}
//temp1和temp2其中有一个为空了,那么把剩下的元素全部插入到REAR中去
while(P1)//如果p1非空
{
P=Attach(P1->coef,P1->expn,P);
P1=P1->next;
}
while(P2)
{
P=Attach(P2->coef,P2->expn,P);
P2=P2->next;
}
polynmail temp=P;
if(temp->coef==0){//第一项系数是0
P=P->next;
free(temp);
}
return P;
}
polynmail MultiplePolyn(polynmail P1,polynmail P2)
{
polynmail x,P2_head;
if(!P1 || !P2)
{
return NULL;
}
int e;
float c;
x=(Node *)malloc(LEN);
x->coef=0;
x->expn=0;
x->next=NULL;
P2_head=P2;
while(P1)
{
while(P2)
{
c = P1->coef * P2->coef;
e = P1->expn + P2->expn;
if(c != 0)//当这一项不是0的时候,加到当前的多项式里去
{
x=Attach(c,e,x);
}
P2=P2->next;
}
P1 = P1->next;
P2=P2_head;
}
polynmail temp=x;
x=x->next;
free(temp);
return x;
}
void PrintPolyn(polynmail P)
{
if(!P)
cout<<“0,0”<<endl;
while§
{
cout<coef<<","<expn<<" “;
P=P->next;
}
cout<<endl;
}
主函数文件:
#include
#include"LinkList.h”
#include<malloc.h>
using namespace std;
int main()
{
polynmail A,B,MulP,AddP;
int m,n;
cout<<“Please enter m and n:”<<endl;
cin>>m>>n;
A=CreatePolyn(m);//输入m项的系数和指数,建立一元多项式有序链表P
B=CreatePolyn(n);
//AddP=AddPolyn(A,B);
//cout<<“A+B=”<<endl;
//PrintPolyn(AddP);
MulP=MultiplePolyn(A,B);
cout<<“A*B=”<<endl;
PrintPolyn(MulP);
return 0;
}