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

来源:互联网 发布:淘宝dnfcdkey码怎么来 编辑:程序博客网 时间:2024/06/05 01:20

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 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三星手机忘了解锁密码怎么办 荣耀8密码忘了怎么办 华为4x开不了机怎么办 华为指纹和密码解锁解不开怎么办 华为荣耀5x死机怎么办 华为荣耀开不了机怎么办 荣耀10开不了机怎么办 乐视pro3变砖了怎么办 手机升级后开不了机怎么办 华为g750开不了机怎么办 手机变砖怎么办插电没反应 变砖手机不通电怎么办 小米手机充电口坏了怎么办 小米2s尾插坏了怎么办 小米手机充电插口坏了怎么办 一条网线上两个亚马逊账号怎么办 加拿大28输20万怎么办 买家账户被亚马逊关闭余额怎么办 京东自营物流慢怎么办 京东退货不给退怎么办 刚付款不想要了怎么办 淘宝卖家拒绝退货退款怎么办 投诉不成立卖家怎么办 淘宝卖家被买家投诉卖假货怎么办 天猫三天未发货怎么办 天猫申请换货卖家不处理怎么办 天猫新疆不发货怎么办 天猫商城少发货怎么办 下单了卖家不发货怎么办 天猫超市漏发货怎么办 天猫购物几天不发货怎么办 天猫总是不发货怎么办 申请退款后卖家又发货了怎么办 天猫拍后申请退款卖家发货怎么办 淘宝上没下单却收到了货怎么办 被买家投诉三无产品怎么办 阿里巴巴卖家虚假发货怎么办 淘宝捡到便宜但是卖家不发货怎么办 被工商局查到三无产品怎么办 淘宝买到三无产品电器怎么办 天猫商城被投诉怎么办