1. 程式人生 > >資料結構篇:連結串列多項式的加法與乘法。(C++)

資料結構篇:連結串列多項式的加法與乘法。(C++)

連結串列多項式還算比較簡單的。

步驟分為 1.建立連結串列

                 2.連結串列排序

                 3.連結串列的自我化簡 

                 4.進行運算

                 5.進行自我化簡(僅乘法需要多這一步)      

首先是尾插法建立兩個連結串列,做加法運算。X前為係數,X後為指數。

然後是尾插法建立兩個連結串列,做加法運算。

附上程式碼

#include <iostream>
using namespace std;
struct Node {
	int coef;
	int exp;
	Node*next;
};
class Poly {
	private:
		Node*Head;
	public:
		Poly();
		void T_Create(int n);
		int TestEmpty();
		int Compare(int a,int b);
		void Port();
		void Simplify();
		void Add(Poly &_poly);
		void Multiply(Poly &_poly);
		void ShowResult();
};
Poly::Poly() {
	Head = new Node;
	Head->next = NULL;
}
void Poly::T_Create(int n) {
	Node *p = Head;
	while(n--) {
		Node*s = new Node;
		cin>>s->coef>>s->exp;
		s->next = p->next;
		p->next = s;
		p = s;
	}
	//先排序,後化簡
	Port();
	Simplify();
	ShowResult();
}
void Poly::Port() { //排序(TODO:的同時進行自我化簡 )
	Node *p=Head->next,*s=p->next;
	while(p) {
		s=p->next;
		while(s) {
			if(p->exp > s->exp) {
				int x,y;
				x=p->coef;
				p->coef=s->coef;
				s->coef=x;
				y=p->exp;
				p->exp=s->exp;
				s->exp=y;
			}
			s=s->next;
		}
		p=p->next;
	}
}
void Poly::Simplify() {
	Node *p=Head->next;
	while(p&&p->next) {
		if(p->exp==p->next->exp) {
			p->coef+=p->next->coef;
			Node *ps=new Node;
			ps=p->next;
			p->next=ps->next;
			delete ps;
		} else {
			p=p->next;
		}
	}
}
int Poly:: Compare(int a,int b) {
	if(a==b)
		return 1;
	else
		return-1;
}
void Poly::Add(Poly &_poly) {
	if(TestEmpty()==0) {
		cout<<"請先建立連結串列"<<endl;
		return ;
	}
	float sum=0;
	Node *pa = Head,
	      *pb = _poly.Head,
	       *qa=Head->next,
	        *qb=_poly.Head->next;
	while(qa!=NULL&&qb!=NULL) {
		if(qa->exp<qb->exp) {
			pa=qa;
			qa=qa->next;
		} else if(qa->exp>qb->exp) {
			pb->next=qb->next;
			qb->next=qa;
			pa->next=qb;
			pa=qb;
			qb=pb->next;
		} else {
			sum=qa->coef+qb->coef;
			if(sum==0) {
				pa->next=qa->next;
				delete qa;
				qa=pa->next;
				pb->next=qb->next;
				delete qb;
				qb=pb->next;
			} else {
				qa->coef=sum;
				pa=qa;
				qa=qa->next;
				pb->next=qb->next;
				delete qb;
				qb=pb->next;
			}
		}
	}
	if(qb!=NULL) {
		pa->next=qb;
	}
}

void Poly::Multiply(Poly &_poly) {
	if(TestEmpty()==0) {
		cout<<"請先建立連結串列"<<endl;
		return ;
	}
	Poly tempPoly1;
	Node *pa = Head->next,
	      *pb = _poly.Head->next,
	       *t1=tempPoly1.Head;
	while(pa) {
		pb=_poly.Head->next;
		while(pb) {
			Node *temp=new Node;
			temp->coef=(pa->coef)*(pb->coef);
			temp->exp = pa->exp+pb->exp;
			temp->next = t1->next;
			t1->next=temp;
			pb = pb->next;
			t1=t1->next;
		}
		pa=pa->next;
	}
	tempPoly1.Port();
	tempPoly1.Simplify();
	pa = Head;
	t1 = tempPoly1.Head->next;
	while(t1) {
		//當還沒超過原連結串列長度時
		if(pa->next) {
			pa->next->coef=t1->coef;
			pa->next->exp=t1->exp;
		}
		//當超過原連結串列長度的時候
		else {
			Node *temp=new Node;
			temp->coef=t1->coef;
			temp->exp=t1->exp;
			temp->next=pa->next;
			pa->next=temp;
		}
		pa=pa->next;
		t1=t1->next;
	}
}
void Poly::ShowResult() {
	Node*p;
	p = Head->next;
	while(p) {
		if(p->coef>0)
			cout<<"+";
		cout<<p->coef<<"X"<<p->exp;
		p = p->next;
	}
	cout<<endl;
}
int Poly:: TestEmpty() {
	Node *p;
	p=Head->next;
	int count=0;
	while(p) {
		count++;
		p=p->next;
	}
	return count;
}
int main() {
	Poly poly,_poly;
	poly.T_Create(2);
	_poly.T_Create(3);
	poly.Add(_poly);
	poly.ShowResult();
	return 0;
}