數據結構和算法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");

}




最終程序的輸出如下所示:



至此,多項式的構建完成了。通過類模板和繼承來實現了稀疏多項式,並且實現了操作符的重載。



  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章