两个多项式相加(顺序表&&链表)

编写一个程序,分别采用顺序表和带头结点的单链表存储一元多项式,并实现两个多项式相加运算,要求:
(1)创建存储多项式的有序表(按无序输入)ha和hb;
(2)求ha和hb相加产生多项式hc。
(3)输出多项式ha、hb和hc;

顺序表完成:

思路及解法:

先定义一个结构体变量Polynomial表示一个项,存放指数和系数,再定义一个顺序表SqList,里面有一个Polynomial的指针和一个表示表长的数据。

输入多项式的函数(利用随机存取的特点,先输入全部项,再排序):

//输入系数指数的函数 (无序输入,输入后要有序)
void CreatePloyn(SqList &L,int n)
{
	cout<<"输入"<>a[i].coef>>a[i].expn;
	}
	int i,j,k;
	//按照指数排序
	for(i=0;i0) cout<<"+";
//		cout<

相加的函数:和线性表合并一样的思路,只不过要注意当指向相同系数互为相反数时,两个节点直接跳过。

void Add(SqList L1,SqList L2,SqList &L3)
{
	int i,j,k;
	i=j=k=0;
	while(iL2.elem[j].expn)
		{
			L3.elem[k].coef=L2.elem[j].coef;
			L3.elem[k].expn=L2.elem[j].expn;
			j++;
			k++;
		}
		else
		{//指数相等时 
			if(L1.elem[i].coef+L2.elem[j].coef!=0)
			{//系数相加为零时
				L3.elem[k].coef=L1.elem[i].coef+L2.elem[j].coef;
			    L3.elem[k].expn=L1.elem[i].expn;
			    k++;
			}
			i++,j++;
		}
	}
	//剩余项直接赋值
	while(i

打印函数:第二项开始,系数大于零时要加'+'

//打印多项式 
void Show_Polyn(SqList L)
{
	int i=0;
	while(i0) cout<<"+";
		cout<

完整代码:

#include
#define MAXSIZE 250
using namespace std;
//定义多项式类型 
typedef struct{
	float coef;
	int expn;
}Polynomial;
typedef struct{
	Polynomial *elem;
	int length;
}SqList;
int InitList(SqList &L)
{
	L.elem=new Polynomial[MAXSIZE];
	if(!L.elem) return 0;
	L.length=0;
	return 1;
}
//输入系数指数的函数 (无序输入,输入后要有序)
void CreatePloyn(SqList &L,int n)
{
	cout<<"输入"<>a[i].coef>>a[i].expn;
	}
	int i,j,k;
	//按照指数排序
	for(i=0;i0) cout<<"+";
//		cout<L2.elem[j].expn)
		{
			L3.elem[k].coef=L2.elem[j].coef;
			L3.elem[k].expn=L2.elem[j].expn;
			j++;
			k++;
		}
		else
		{//系数相等时 
			if(L1.elem[i].coef+L2.elem[j].coef!=0)
			{
				L3.elem[k].coef=L1.elem[i].coef+L2.elem[j].coef;
			    L3.elem[k].expn=L1.elem[i].expn;
			    k++;
			}
			i++,j++;
		}
	}
	//剩余项直接赋值
	while(i0) cout<<"+";
		cout<>n;
	CreatePloyn(L1,n);
	cout<<"第二个多项式的项数:";
	cin>>n;
	CreatePloyn(L2,n);
	//相加打印
//	//test
//	cout<<"第一个多项式:\n";
//	Show_Polyn(L1);
//	cout<<"第二个多项式:\n";
//	Show_Polyn(L2);
	Add(L1,L2,L3); 
	cout<<"第一个多项式:\n";
	Show_Polyn(L1);
	cout<<"第二个多项式:\n";
	Show_Polyn(L2);
	cout<<"相加:\n";
	Show_Polyn(L3);
	
}

链表完成

节点定义:

//定义多项式节点 
typedef struct PNode{
	int expn;
	float coef;
	struct PNode *next;
}PNode,*Polynomial;

输入函数:

没有顺序表那种随机存取的特点,无序输入链表每一个节点时都要插入第一个小于(指系数小于)输入节点的节点,相等时系数加入即可。

void CreatePolyn(Polynomial &p,int n)
{
	Polynomial prev,q,s;
	p=new PNode;
	p->next=NULL;
	for(int i=0;inext=NULL;
		cout<<"输入多项式的第"<>s->coef>>s->expn;
		prev=p;
		q=p->next;
		while(q&&q->expnexpn){
			prev=q;
			q=q->next;
		}
		if(q==NULL){//新节点是最大的
			s->next=q;
			prev->next=s;
		}else if(q->expn==s->expn){//有指数相等的节点
			float sum=q->coef+s->coef;
			if(sum==0){
				prev->next=q->next;
				delete q;
				delete s;
			}else{
				q->coef+=s->coef;
				delete s;
			}
		}else{
			s->next=q;
			prev->next=s;
		}
	}
}

相加函数:

链表有序后,相加函数就和两个有序链表的合并一样简单。只不过要注意指数相同系数相加为零的情况。

void AddPolyn(Polynomial &pa,Polynomial &pb,Polynomial &pc)
{
	CreatePolyn(pc,0);
	Polynomial p1,p2,p3,s,q;
	p1=pa->next;p2=pb->next;p3=pc;
	while(p1&&p2){
		if(p1->expn==p2->expn&&p1->coef+p2->coef==0){//指数相同并且相加为零
			s=p1;
			p1=p1->next;
			delete s;
			s=p2;
			p2=p2->next;
			delete s;
		}
		else if(p1->expn==p2->expn&&p1->coef+p2->coef!=0){//指数相等且系数和不为零 
			s=new PNode;
			s->coef=p1->coef+p2->coef;s->expn=p1->expn;s->next=NULL;  //复制数据域 
			p3->next=s;p3=s;                            //后插入p3中 
			p1=p1->next;
			p2=p2->next;
		}else if(p1->expnexpn){
			s=new PNode;
			s->coef=p1->coef;s->expn=p1->expn;s->next=NULL;
			p3->next=s;p3=s;
			p1=p1->next;
		}else{
			s=new PNode;
			s->coef=p2->coef;s->expn=p2->expn;s->next=NULL;
			p3->next=s;p3=s;
			p2=p2->next;
		}
	}
	p3->next=p1?p1:p2;//赋值剩余项      
}

完整代码:

//稀疏多项式的运算 
#include
using namespace std;
//定义多项式节点 
typedef struct PNode{
	int expn;
	float coef;
	struct PNode *next;
}PNode,*Polynomial;
void CreatePolyn(Polynomial &p,int n)
{
	Polynomial prev,q,s;
	p=new PNode;
	p->next=NULL;
	for(int i=0;inext=NULL;
		cout<<"输入多项式的第"<>s->coef>>s->expn;
		prev=p;
		q=p->next;
		while(q&&q->expnexpn){
			prev=q;
			q=q->next;
		}
		if(q==NULL){
			s->next=q;
			prev->next=s;
		}else if(q->expn==s->expn){
			float sum=q->coef+s->coef;
			if(sum==0){
				prev->next=q->next;
				delete q;
				delete s;
			}else{
				q->coef+=s->coef;
				delete s;
			}
		}else{
			s->next=q;
			prev->next=s;
		}
	}
}
void AddPolyn(Polynomial &pa,Polynomial &pb,Polynomial &pc)
{
	CreatePolyn(pc,0);
	Polynomial p1,p2,p3,s,q;
	p1=pa->next;p2=pb->next;p3=pc;
	while(p1&&p2){
		if(p1->expn==p2->expn&&p1->coef+p2->coef==0){
			s=p1;
			p1=p1->next;
			delete s;
			s=p2;
			p2=p2->next;
			delete s;
		}
		else if(p1->expn==p2->expn&&p1->coef+p2->coef!=0){//指数相等且系数和为非零 
			s=new PNode;
			s->coef=p1->coef+p2->coef;s->expn=p1->expn;s->next=NULL;  //复制数据域 
			p3->next=s;p3=s;                            //后插入p3中 
			p1=p1->next;
			p2=p2->next;
		}else if(p1->expnexpn){
			s=new PNode;
			s->coef=p1->coef;s->expn=p1->expn;s->next=NULL;
			p3->next=s;p3=s;
			p1=p1->next;
		}else{
			s=new PNode;
			s->coef=p2->coef;s->expn=p2->expn;s->next=NULL;
			p3->next=s;p3=s;
			p2=p2->next;
		}
	}
	p3->next=p1?p1:p2;//赋值剩余项      
}
void ShowPolyn(Polynomial &p)
{
	Polynomial pt=p->next;
	while(pt){
		if(pt!=p->next&&pt->coef>0) cout<<"+";
		cout<coef<<"X^"<expn;
		pt=pt->next;
	}
//	//test
//	pt=p;
//	while(pt){
//		cout<<"\n当前指针:"<next;
//		pt=pt->next;
//	}
}
int main()
{
	Polynomial pa,pb,pc;
	int n,m;
	cout<<"第一个多项式的项数:";
	cin>>n;
	CreatePolyn(pa,n);
	ShowPolyn(pa);
	cout<<"\n第二个多项式的项数:";
	cin>>m;
	CreatePolyn(pb,m);
	ShowPolyn(pb);
	cout<<"\n相加结果为:";
	AddPolyn(pa,pb,pc);
	ShowPolyn(pc);
}

你可能感兴趣的:(数据结构学习记录,链表,数据结构,c++)