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

1、链表的基础知识

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

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

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

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

template 
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
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、该实例化类的对象之间存在赋值,拷贝,判断相等的操作

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

template
class CPolynomial :public List>
{
public:

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

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

	template
	friend CPolynomial operator-(const CPolynomial& polynLeft,const CPolynomial &polynRight)
	{
		CPolynomial polynTemp = polynLeft;
		polynTemp.PolynSubstract(polynRight);

		return polynTemp;
	}

	friend CPolynomial operator*(const CPolynomial &polyn)
	{

	}

	void PolynInsert(const Item & item);
	void PolynRemove(const Item & item);
	bool PolynSearch(const Item &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> m_List;
};



 
  


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

#include "List.h"
#include "List.cpp"
#include "Polynomial.h"
#include "Polynomial.cpp"

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

	myList.List_Insert(15);
	cout<<"**myList:"< newList = myList;
	cout<<"**newList:"< newOpList;
	newOpList = myList;
	cout<<"**newOpList:"< item;
	CPolynomial 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 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 m_PolynThird;
	m_PolynThird = m_Polyn + m_PolynSecond;
	cout << "+:" << endl;
	m_PolynThird.PolynPrint();

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

}


 
  


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


数据结构和算法C++语言实现:使用链表实现稀疏多项式_第1张图片

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



  

你可能感兴趣的:(C++数据结构和算法)