码迷,mamicode.com
首页 > 其他好文 > 详细

多项式链表多项式相加

时间:2016-09-20 01:44:06      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

//多项式相加
LinkList* add2Link(LinkList*L1, LinkList*L2)
{
	LinkList*p1, *p2;
	LinkList*head , *pre, *p;
	head = new LinkList;
	head->next = NULL;
	pre = head;
	p1 = L1->next;
	p2 = L2->next;
	while (p1&&p2)
	{
		if (p1->exp == p2->exp)
		{
			p = new LinkList;
			p->coef = p1->coef + p2->coef;
			p->exp = p1->exp;
			p1 = p1->next;
			p2 = p2->next;
		}
		else if ((p1->exp) < (p2->exp))
		{
			p = new LinkList;
			p->coef = p1->coef;
			p->exp = p1->exp;
			p1 = p1->next;
		}
		else
		{
			p = new LinkList;
			p->coef = p2->coef;
			p->exp = p2->exp;
			p2 = p2->next;
		}
		pre->next = p;
		pre = p;
	}
	if (p1&&!p2)
	{
		while (p1)
		{
			p = new LinkList;
			p->coef = p1->coef;
			p->exp = p1->exp;
			p1 = p1->next;
			pre->next = p;
			pre = p;
		}
	}
	else if (!p1&&p2)
	{
		while (p2)
		{
			p = new LinkList;
			p->coef = p2->coef;
			p->exp = p2->exp;
			p2 = p2->next;
			pre->next = p;
			pre = p;
		}
	}
	pre->next = NULL;
	return head;
}

  

 

多项式链表多项式相加

标签:

原文地址:http://www.cnblogs.com/KennyRom/p/5887309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!