0
点赞
收藏
分享

微信扫一扫

链表实现多项式加法c++(初学数据结构,仅供参考)

无愠色 2022-03-11 阅读 52

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

本蒟蒻刚刚学完链表,老师让自己实现多项式加法,若有错误,希望各位指正


提示:以下是本篇文章正文内容

一、原理

链表的每一个结点存放三个数据元素,一个是指数(int),一个是系数(int),一个是next指针(NODE*),相加时用两个指针分别从首结点开始比较指数,对于指数较小的那个结点,将那一项放入新的多项式,然后指针向后移。如果两个结点指数相等,则将系数相加,存入新的多项式,然后指针后移。知道一个链表遍历结束。

二、

1.多项式链表定义:

用模板感觉用处不大,但要求用模板实现,希望有人可以告诉我模板有什么用

template <class elemType>
class Polynomial
{
private:
	struct NODE
	{
		int exp;  //指数
		int coef; //系数
		NODE*next;
		NODE(const int &x, const int&y, NODE*p = NULL) :next(p), exp(x), coef(y) {}
		NODE() :next(NULL) {}
	};
	NODE* head;
	elemType stop_flag; // 用于判断多项式输入结束。
public:
	//从用户处获取结束标志并初始化多项式
	Polynomial();
	Polynomial(const elemType &stop);
	void getPoly(); //读入一个多项式
	Polynomial addPoly(const Polynomial &L1, const Polynomial &L2) const;//L1+L2
	void dispPloy();//显示一个多项式
	void clear();//释放多项式空间
	~Polynomial();
};

2.函数实现

代码如下:

1、构造函数:

template<class elemType>
Polynomial<elemType>::Polynomial(const elemType&stop)
{
	head = new NODE();
	stop_flag = stop;
}
template<class elemType>
Polynomial<elemType>::Polynomial()
{
	head = new NODE();
}

2、获取多项式:

void Polynomial<elemType>::getPoly()
{

	elemType exponential =0, coefficient =0;//用来接收输入的数据
	NODE*p = head;
	while (coefficient != stop_flag && exponential != stop_flag)
	{
		cin >> exponential;
		if (exponential == stop_flag) return;
		cin >> coefficient;
        //插入
		NODE*tmp = new NODE(exponential, coefficient, p->next);
		p->next = tmp;
		p = tmp;
	}
}

3、相加:

template<class elemType>
Polynomial<elemType> Polynomial<elemType>::addPoly(const Polynomial<elemType> &L1, const Polynomial<elemType> &L2) const
{
	Polynomial<elemType> res;
	NODE*p = L1.head->next, *q = L2.head->next, *t = res.head;
	if (!p || !q) throw errortype1();  //如果有多项式为空,抛出异常
	while (p != NULL && q != NULL)
	{
		if (p->exp < q->exp)
		{
			NODE *tmp = new NODE(p->exp, p->coef, t->next);
			t->next = tmp;
			t = tmp;
			p = p->next;
		}
		else if (p->exp > q->exp)
		{
			NODE *tmp = new NODE(q->exp, q->coef, t->next);
			t->next = tmp;
			t = tmp;
			q = q->next;
		}
		else
		{
			NODE *tmp = new NODE(q->exp, q->coef + p->coef, t->next);
			t->next = tmp;
			t = tmp;
			p = p->next;
			q = q->next;
		}
	}
//把剩下的加上
if (p == NULL && q != NULL)
	{
		while (q)
		{
			NODE *tmp = new NODE(q->exp, q->coef, t->next);
			t->next = tmp;
			t = tmp;
			q = q->next;
		}
	}
	else if (p != NULL && q == NULL)
	{
		while (p)
		{
			NODE *tmp = new NODE(p->exp, p->coef, t->next);
			t->next = tmp;
			t = tmp;
			p = p->next;
		}
	}
	return res;
}

4、显示数组:
 

template<class elemType>
void Polynomial<elemType>::dispPloy()
{
	NODE*p = head->next;
	if (!p)return;//多项式不存在直接return
	while (p->next)
	{
		if (p->exp == 0)
			cout << p->coef << "+";
		else if (p->exp == 1)
			cout << p->coef << "x" << "+";
		else
			cout << p->coef << "x" << "^" << p->exp << "+";
		p = p->next;
	}
	if (p->exp == 0)
		cout << p->coef;
	else if (p->exp == 1)
		cout << p->coef << "x";
	else
	cout << p->coef << "x" << "^" << p->exp << endl;
	delete p;

}

5、清空:

template<class elemType>
void Polynomial<elemType>::clear()
{
    if (!head->next) return;
    NODE*p = head->next, *q;
    head->next = NULL;
    while (p)
    {
        q = p->next;
        delete p;
        p = q;
    }
}

3、main函数操作:
 

int main()
{
	try
	{
	int s1, s2;
	cin >> s1 >> s2;
	Polynomial<int> p1(s1), p2(s2);
	p1.getPoly();
	p2.getPoly();
	Polynomial<int> p3;
	p3=p3.addPoly(p1, p2);
	p3.dispPloy();
	p1.clear();
	p2.clear();
	p3.clear();
    }
	catch(errortype1)
	{
	cout<<"多项式不存在,不可以相加"<<endl; 
    } 

	return 0;
}

测试:
 

999
888
0
7
1
1
3
4
5
5
999
0
6
2
1
3
4
7
6
9
8
888
结果:
13+1x+1x^2+8x^3+5x^5+6x^7+8x^9

总结

代码可能有问题,希望大佬们可以帮忙指正

举报

相关推荐

0 条评论