数据结构和算法C++语言实现:使用链表实现稀疏多项式

1、链表的基础知识

       关于链表的基础知识还有不熟悉的,请查看链表的实现(基于动态内存分配)

2、使用链表实现稀疏多项式,也是基于上述的链表来实现的。

       稀疏多项式可以当作一种链表结构,所以,可以直接从上述的链表派生过来。在派生之前应该考虑清楚稀疏多项式的结构,稀疏多项式的基本单元由系数指数构成,所以构成该类的模板需要两个参数,声明为如下的形式:

    template<typename T,typename S>
       class CPolynomial
        由于CPolynomial是从CList继承过来的,CList的声明如下所示:

template <typename T>
class List
{
protected:
	class Node
	{
	public:
		Node *pNext;
		T m_data;

		Node()
		{
			memset(&m_data, 0, sizeof(m_data));
			pNext = NULL;
		}
	};
	//typedef Node * Link_List;
	int m_Lenth;
	
public:
	List();
	List(const List &m_List);
	~List();
	const List&operator=(const List &m_List);

    bool List_IsEmpty();
	int  List_Lenth();
	void List_Insert(T num);
	void List_Delete(int nPos);
	void List_Destroy();
	void List_Clear();
	void List_Display();
	Node * List_GetHead() const;
	bool List_FindElem(T num);
	void List_Modify(T num, int nPos);

protected:

	//Link_List pHead;
	Node *pHead;
	typedef Node* Position;
};

#endif
 在构建CPolynomial时,还需要考虑到基类的模板的参数化的问题,所以,这里需要提供一个模板类,用以实例化CList,定义如下:

template<typename T,typename S>
class Item
{
public:
	T coeff;
	S index;

	Item()
	{
		coeff = 0;
		index = 0;
	}
	Item(const Item& item)
	{
		coeff = item.coeff;
		index = item.index;
	}

	Item & operator = (const Item& item)
	{

		coeff = item.coeff;
		index = item.index;

		return *this;
	}

	bool operator == (const Item& item)
	{
		return coeff == item.coeff&& index == item.index;
	}
};
为什么要按照上述的方法来定义呢?从如下的方面来考虑的:

1、该实例类需要和CPolynomial中的指数和系数对应

2、该实例化类的对象之间存在赋值,拷贝,判断相等的操作

到目前,构建该多项式类的基本工作已经完成了,下面需要来实际构建该类:

<pre name="code" class="cpp">template<typename T,typename S>
class CPolynomial :public List<Item<T,S>>
{
public:

	CPolynomial();
	CPolynomial(const CPolynomial& poly);
	~CPolynomial();

	//操作符重载
	const CPolynomial &operator =(const CPolynomial &polyn);
	//friend CPolynomial operator+(const CPolynomial &polyn);
    //①friend CPolynomial operator+<>(const CPolynomial<T,S>& polynLeft,const CPolynomial<T,S> &polynRight);
	
	template<typename T,typename S>
	friend CPolynomial<T, S> operator+(const CPolynomial<T, S>&, const CPolynomial<T, S>&);

	template<typename T,typename S>
	friend CPolynomial<T,S> operator-(const CPolynomial<T,S>& polynLeft,const CPolynomial<T,S> &polynRight)
	{
		CPolynomial<T, S> polynTemp = polynLeft;
		polynTemp.PolynSubstract(polynRight);

		return polynTemp;
	}

	friend CPolynomial operator*(const CPolynomial &polyn)
	{

	}

	void PolynInsert(const Item<T,S> & item);
	void PolynRemove(const Item<T,S> & item);
	bool PolynSearch(const Item<T,S> &item,int &nPos);
	bool PolynSearchIndex(const S& index,Position *pos = NULL);
	bool PolynModify(Position pos);
	bool PolynIsEmpty();
	void PolynClear();
	void PolynDestroy();
	void PolynPrint();

	void PolynAdd(const CPolynomial &polynSecond);
	void PolynSubstract(CPolynomial polynSecond);
	void PolynMultiply(CPolynomial polynFirst, CPolynomial polynSecond);

public:
	int m_nLenth;
	List<Item<T,S>> m_List;
};



 


上述的多项式类至此已经构建完成,接下来便进行相关的测试:

<pre name="code" class="cpp">#include "List.h"
#include "List.cpp"
#include "Polynomial.h"
#include "Polynomial.cpp"

void main()
{
	List<int> myList;
	for(int i=0;i<12;i++)
	{
		myList.List_Insert(i);
	}

	myList.List_Insert(15);
	cout<<"**myList:"<<myList.List_Lenth()<<endl;
	myList.List_Display();

	List<int> newList = myList;
	cout<<"**newList:"<<newList.List_Lenth()<<endl;
	newList.List_Display();

	myList.List_Insert(-2);
	List<int> newOpList;
	newOpList = myList;
	cout<<"**newOpList:"<<newOpList.List_Lenth()<<endl;
	newOpList.List_Display();


	for(int i=0;i<5;i++)
		newOpList.List_Delete(1);
	cout<<"**newOpList:"<<newOpList.List_Lenth()<<endl;
	newOpList.List_Display();
	
	newOpList.List_Destroy();
     
	Item<double, int> item;
	CPolynomial<double, int> m_Polyn;
	for (int i = 1; i < 6; i++)
	{
		item.coeff = i*3.14;
		item.index = i;
		m_Polyn.PolynInsert(item);
	}

	m_Polyn.PolynPrint();

	CPolynomial<double, int> m_PolynSecond;
	for (int i = 3; i < 8; i++)
	{
		item.coeff = i*i;
		item.index = i;
		m_PolynSecond.PolynInsert(item);
	}

	cout << "Second:" << endl;
	m_PolynSecond.PolynPrint();

	
	m_Polyn.PolynAdd(m_PolynSecond);
	cout << "Add:" << endl;
	m_Polyn.PolynPrint();

	CPolynomial<double, int> m_PolynThird;
	m_PolynThird = m_Polyn + m_PolynSecond;
	cout << "+:" << endl;
	m_PolynThird.PolynPrint();

	CPolynomial<double, int> m_PolynForth;
	m_PolynForth = m_PolynThird - m_PolynSecond;
	cout << "Forth:" << endl;
	m_PolynForth.PolynPrint();
	system("pause");

}


 


最终程序的输出如下所示:



至此,多项式的构建完成了。通过类模板和继承来实现了稀疏多项式,并且实现了操作符的重载。



  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值