一元多项式相加-链表应用

一元多项式相加,相减与相加类似。

链表应用

完整代码:

#include<iostream>
#include<malloc.h>
#include<cstring>
using namespace std;

typedef struct LNode
{
	struct LNode *next;
	int exp;  //index
	int coef;  //coefficient
}*Linklist,LNode;

void InitLinklist(Linklist &L)
{
	L=(LNode*)malloc(sizeof(LNode));
	L->next=NULL;
}

void CreatPloy(Linklist &L)
{
	Linklist p,q;
	int co,ex;
	p=L;
	cout<<"Input the index and coefficient, 0 0 to end."<<endl;
	while(true) // end
	{
		cin>>co>>ex;
		if(!co) break;  //0x^b  end input
		q=(LNode*)malloc(sizeof(LNode));
		q->coef=co;
		q->exp=ex;
		p->next=q;
		p=q;
	}
	p->next=NULL;
}
Linklist AddPloy(Linklist L1,Linklist L2)
{
	Linklist L,p,q,r,s;
	r=L=(LNode*)malloc(sizeof(LNode));
	p=L1->next;
	q=L2->next;
	while(p!=NULL && q!=NULL)
	{
		if(p->exp==q->exp)
		{
			if((p->coef+q->coef)!=0)  //pass ax^b+cx^b=0x^b
			{
				s=(LNode*)malloc(sizeof(LNode));
				s->coef=p->coef+q->coef;
				s->exp=p->exp;
				r->next=s;
				r=s;
			}
			p=p->next;
			q=q->next;
		}
		else if(p->exp>q->exp)
		{
			r->next=q;
			r=q;
			q=q->next;
		}
		else
		{
			r->next=p;
			r=p;
			p=p->next;
		}
	}
	while(p!=NULL)
	{
		r->next=p;
		r=p;
		p=p->next;
	}
	while(q!=NULL)
	{
		r->next=q;
		r=q;
		q=q->next;
	}
	r->next=NULL;
	return L;
}

Linklist MinusPloy(Linklist L1,Linklist L2)
{
	Linklist p,q,r,L,s;
	r=L=(LNode*)malloc(sizeof(LNode));
	p=L1->next;
	q=L2->next;
	while(p!=NULL && q!=NULL)
	{
		if(p->exp==q->exp)
		{
			if((p->coef-q->coef)!=0)
			{
				s=(LNode*)malloc(sizeof(LNode));
				s->coef=p->coef-q->coef;
				s->exp=p->exp;
				r->next=s;
				r=s;
			}
			p=p->next;
			q=q->next;
		}
		else if(p->exp>q->exp)
		{
			q->coef=-q->coef;
			r->next=q;
			r=q;
			q=q->next;
		}
		else
		{
			r->next=p;
			r=p;
			p=p->next;
		}
	}
	while(p!=NULL)
	{
		r->next=p;
		r=p;
		p=p->next;
	}
	while(q!=NULL)
	{
		q->coef=-q->coef;
		r->next=q;
		r=q;
		q=q->next;
	}
	r->next=NULL;
	return L;
}

int main()
{
	Linklist Link1,Link2,p,Link;
	cout<<"Creat Ploy 1"<<endl;
	InitLinklist(Link1);
	CreatPloy(Link1);
	cout<<"Creat Ploy 2"<<endl;
	InitLinklist(Link2);
	CreatPloy(Link2);
	cout<<"The two ploy:"<<endl;
	for(p=Link1->next;p->next!=NULL;p=p->next)
		cout<<p->coef<<"X^"<<p->exp<<"+";
		cout<<p->coef<<"X^"<<p->exp<<endl;
	cout<<endl;
	for(p=Link2->next;p->next!=NULL;p=p->next)
		cout<<p->coef<<"X^"<<p->exp<<"+";
		cout<<p->coef<<"X^"<<p->exp<<endl;
	cout<<endl;

	cout<<"Now Ploy1+Ploy2:"<<endl;
	Link=AddPloy(Link1,Link2);
	cout<<"After add:"<<endl;
	for(p=Link->next;p->next!=NULL;p=p->next)
		cout<<p->coef<<"X^"<<p->exp<<"+";
		cout<<p->coef<<"X^"<<p->exp<<endl;
	cout<<endl;



	Link=MinusPloy(Link1,Link2);
	cout<<"After Minus:"<<endl;

	for(p=Link->next;p->next!=NULL;p=p->next)
		cout<<p->coef<<"X^"<<p->exp<<"+";
		cout<<p->coef<<"X^"<<p->exp<<endl;
	cout<<endl;

	/*cout<<"The two ploy:"<<endl;
	for(p=Link1->next;p->next!=NULL;p=p->next)
		cout<<p->coef<<"X^"<<p->exp<<"+";
		cout<<p->coef<<"X^"<<p->exp<<endl;
	cout<<endl;
	for(p=Link2->next;p->next!=NULL;p=p->next)
		cout<<p->coef<<"X^"<<p->exp<<"+";
		cout<<p->coef<<"X^"<<p->exp<<endl;
	cout<<endl;*/

	return 0;
}


  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值