风言枫语  

重新看数据结构了,然后第二章线性表多项式老师没讲自己看的,什么鸡巴学校啊。

最后实现的时候系数0没考虑,其实主要的指针操作已经出来了。不过少了一个系数为0的节点应该删去,少了删除操作。

 

#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
	int xi;
	int ci;
	node* next;
};
int main()
{
	int lengtha,lengthb,i;
	node *L1,*L2,*p,*q;
	cin>>lengtha>>lengthb;
	L1=(node*)malloc(sizeof(node));
	L2=(node*)malloc(sizeof(node));
	L1->next=NULL;
	L2->next=NULL;
	p=L1;
	for(i=1;i<=lengtha;i++)
	{
		q=(node*)malloc(sizeof(node));
		cin>>q->xi>>q->ci;
		p->next=q;
		p=q;
		p->next=NULL;
	}
	p=L2;
	for(i=1;i<=lengthb;i++)
	{
		q=(node*)malloc(sizeof(node));
		cin>>q->xi>>q->ci;
		p->next=q;
		p=q;
		p->next=NULL;
	}
	p=L1->next;
	q=L2->next;
	while(p!=NULL&&q!=NULL)
	{
		if(p->ci==q->ci)
		{
			p->xi=p->xi+q->xi;
			p=p->next;
			q=q->next;
		}
		else if(p->ci>q->ci)
		{
			node *j,*n;
			j=L1;
			n=q;
			q=q->next;
			while((j->next)!=p)
			{
				j=j->next;
			}
			n->next=j->next;
			j->next=n;
		}
		else p=p->next;
	}
	if(p==NULL&&q!=NULL)
		p=q;

	p=L1->next;
	while(p!=NULL&&p->next!=NULL)
	{
		cout<<p->xi<<"x^"<<p->ci<<"+";
		p=p->next;
	}
	if(p!=NULL&&p->next==NULL)
		cout<<p->xi<<"x^"<<p->ci<<endl;
	system("pause");
	return 0;
}







 

 

posted on 2013-10-08 22:52  风言枫语  阅读(707)  评论(0编辑  收藏  举报