数据结构-多项式的基本操作(链表实现)

#include
#include
using namespace std;
#define MAX 100

typedef struct 
{
	double coef;	//系数
	int exp;		//指数
}PolyArray;			//存放多项式的数组类型
typedef struct pnode
{
	double coef;	//系数
	int exp;		//指数
	struct pnode* next;
}PolyNode;			//声明多项式单链表结点类型

void DispPoly(PolyNode* L)		//输出多项式单链表
{
	bool first = true;			//first为true表示第一项
	PolyNode* p = L->next;
	while (p != NULL)
	{
		if (first)
			first = false;
		else if (p->coef > 0)
			cout << "+";
		if (p->exp == 0)
			printf("%g", p->coef);
		else if(p->exp==1)
			printf("%gx", p->coef);
		else
			printf("%gx^%d", p->coef, p->exp);
		p = p->next;
	}
	cout << endl;
}

void DestoryPoly(PolyNode* &L)			//销毁多项式单链表
{
	PolyNode* pre = L, * p = pre->next;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = pre->next;
	}
	free(pre);
}

void CreataPolyR(PolyNode*& L, PolyArray a[], int n)			//尾插法建表
{
	PolyNode* s, * r;
	int i;
	L = (PolyNode*)malloc(sizeof(PolyNode));			//创建头结点
	L->next = NULL;
	r = L;			//r始终指向尾结点,开始时指向头结点
	for (i = 0; i < n; i++)
	{
		s= (PolyNode*)malloc(sizeof(PolyNode));			//创建新结点
		s->coef = a[i].coef;
		s->exp = a[i].exp;
		r->next = s;								//将结点s插入到结点r之后
		r = s;
	}
	r->next = NULL;									//尾结点next域置为空
}

void Sort(PolyNode*& L)					//将多项式单链表按指数递减排序
{
	PolyNode* p = L->next, * pre, * q;
	if (p != NULL)
	{
		q = p->next;			//q保存p结点的后继结点
		p->next = NULL;			//构造只含一个数据结点的有序表
		p = q;
		while(p != NULL)
		{
			q = p->next;			//q保存p结点的后继结点
			pre = L;
			while (pre->next != NULL && pre->next->exp > p->exp)
				pre = pre->next;		//在有序表中找插入结点p的前驱结点pre
			p->next = pre->next;
			pre->next = p;
			p = q;						//扫描原单链表余下的结点
		}
	}
}

void Add(PolyNode* ha, PolyNode* hb, PolyNode*& hc)		//ha和hb相加得到hc
{
	PolyNode* pa = ha->next, * pb = hb->next, * s, * r;
	double c;
	hc = (PolyNode*)malloc(sizeof(PolyNode));
	r = hc;				//r始终指向尾结点,开始时指向头结点
	while (pa != NULL && pb != NULL)			//pa,pb均没有扫描完
	{//下面开始进行比较,按照指数的大小来比较
		if (pa->exp > pb->exp)				//将指数较大的pa结点复制到hc中
		{
			s = (PolyNode*)malloc(sizeof(PolyNode));
			s->exp = pa->exp; s->coef = pa->coef;
			r->next = s;
			r = s;
			pa = pa->next;
		}
		else if (pa->exp < pb->exp)				//将指数较大的pb结点复制到hc中
		{
			s = (PolyNode*)malloc(sizeof(PolyNode));
			s->exp = pb->exp; s->coef = pb->coef;
			r->next = s;
			r = s;
			pb = pb->next;
		}
		else                                    //pa,pb的指数相等时
		{
			c = pa->coef + pb->coef;			//两个结点的系数和为c
			if (c != 0)                         //若系数和不为0时创建新结点
			{
				s = (PolyNode*)malloc(sizeof(PolyNode));
				s->exp = pa->exp; s->coef = c;
				r->next = s;
				r = s;
			}
			pa = pa->next;				//pa,pb均后移一个结点
			pb = pb->next;
		}
	}
	if (pb != NULL)pa = pb;				//复制余下的结点
	while (pa != NULL)
	{
		s = (PolyNode*)malloc(sizeof(PolyNode));
		s->exp = pa->exp; s->coef =pa->coef;
		r->next = s;
		r = s;
		pa = pa->next;
	}
	r->next = NULL;			//尾结点next置为空
}

void Mult1(PolyNode* ha, PolyNode* hb, PolyNode*& hc)		//ha,hb简单相乘得到hc
{
	PolyNode* pa = ha->next, * pb, * s, * tc;
	hc = (PolyNode*)malloc(sizeof(PolyNode));
	tc = hc;
	while (pa != NULL)
	{
		pb = hb->next;
		while (pb != NULL)
		{
			s= (PolyNode*)malloc(sizeof(PolyNode));
			s->coef = pa->coef * pb->coef;
			s->exp = pa->exp + pb->exp;
			tc->next = s;
			tc = s;
			pb = pb->next;
		}
		pa = pa->next;
	}
	tc->next = NULL;
}

void Comb(PolyNode*& L)			//合并指数相同的项
{
	PolyNode* pre = L->next, * p;
	if (pre == NULL)return;
	p = pre->next;
	while (p != NULL)
	{
		while (p->exp == pre->exp)
		{
			pre->coef += p->coef;
			pre->next = p->next;
			free(p);
			p = pre->next;
		}
		pre = p;
		p = p->next;
	}
}

void DelZero(PolyNode*& L)		//删除系数为0的项
{
	PolyNode* pre = L, * p = pre->next;
	while (p != NULL)
	{
		if (p->coef == 0.0)
		{
			pre->next = p->next;
			free(p);
		}
		pre = p;
		p = p->next;
	}
}

void Mult(PolyNode* ha, PolyNode* hb, PolyNode*& hc)		//ha,hb相乘得到最终的hc
{
	Mult1(ha, hb, hc);
	cout << "相乘结果: "; DispPoly(hc);
	Sort(hc);
	cout << "按指数排序后: "; DispPoly(hc);
	Comb(hc);
	cout << "合并重复指数项: "; DispPoly(hc);
	DelZero(hc);
	cout << "删除系数为0的项: "; DispPoly(hc);
}

int main()
{
	PolyNode* Poly1, * Poly2, * Poly3;
	int  n;
	//----创建第1个多项式单链表并排序----
	PolyArray a[] = { {2,3},{1,0},{3,1} };
	n = 3;
	cout << "第1个多项式: " << endl;
	CreataPolyR(Poly1, a, n);
	cout << "排序前多项式1: "; DispPoly(Poly1);
	Sort(Poly1);
	cout << "排序后多项式1: "; DispPoly(Poly1);
	cout << endl;
	//----创建第2个多项式单链表并排序----
	PolyArray b[] = { {2,3},{-3,2},{5,4},{-3,0 } };
	n = 4;
	cout << "第2个多项式: " << endl;
	CreataPolyR(Poly2, b, n);
	cout << "排序前多项式2: "; DispPoly(Poly2);
	Sort(Poly2);
	cout << "排序后多项式2: "; DispPoly(Poly2);
	cout << endl;
	Mult(Poly1, Poly2, Poly3);
	cout << "相乘后多项式3: "; DispPoly(Poly3);
	cout << endl;
	cout << "最后的A: "; DispPoly(Poly1);
	cout << "最后的B: "; DispPoly(Poly2);
	cout << "最后的C: "; DispPoly(Poly3);
	cout << endl;
	cout << "20213002624李季鸿代码";
	system("pause");
	return 1;
}

你可能感兴趣的:(数据结构)