C++算法与数据结构学习笔记------单链表实现多项式

//使用单链表实现了多项式的加减和乘法。
#include<iostream.h>
template<class T>
class List;
template<class T>
class Node{
   friend class List<T>;
   private:
       T coef,exp;
       Node<T> *next;
};
template<class T>
class List{
    private:
    Node<T> *first;
    public:
        List(){first=0;}
        ~List();
        bool Empty() const{return first==0;}
        int Length()const;
        int Locate(const T& c,const T& x)const;
        bool Retrieve(int k,T& c, T& x)const;
        List<T>& Insert(int k,const T& c, const T& x);
        List<T>& Delete(int k,T& c,T& x);
        void PrintList();
        List<T>& polyAdd(List<T>& poly2);
        List<T>& polyMul(List<T>& poly2,List<T>& poly3);
        List<T>& mergerPoly();
};
template<class T>
List<T>::~List()
{
  Node<T> *next;
  while(first){
     next=first->next;
     delete first;
     first=next;
  }
}
template<class T>
int List<T>::Length()const
{
    Node<T> *current=first;
    int len=0;
    while(current){
       len++;
       current=current->next;
    }
    return len;
}
template<class T>
int List<T>::Locate(const T& c,const T& x)const
{
  Node<T> *current=first;
  int index=1;
  while(current&¤t->coef!=c&¤t->exp!=x){
    current=current->next;
    index++;
  }
  if(current)return index;
  return 0;
}
template<class T>
bool List<T>::Retrieve(int k,T& c,T& x)const
{
  if(k<1)return false;
  Node<T> *current=first;
  int index=1;
  while(index<k&¤t){
    current=current->next;
    index++;
  }
  if(current){
     c=current->coef;
          x=current->exp;
     return true;
  }
  return false;
}
template<class T>
List<T>& List<T>::Insert(int k,const T& c,const T& x)
{
   Node<T> *p=first;
   for(int index=1;index<k&&p;index++)
      p=p->next;

   Node<T> *y=new Node<T>;
   y->coef=c;
   y->exp=x;
   if(k){
      y->next=p->next;
      p->next=y;
   }
   else{
      y->next=first;
      first=y;
   }
   return *this;
}
template<class T>
List<T>& List<T>::Delete(int k,T& c,T& x)
{
   Node<T> *p=first;
   if(k==1)
       first=first->next;
   else{
       Node<T> *q=first;
       for(int index=1;index<k-1&&q;index++)
           q=q->next;
       if(!q||!q->next) return *this;
       p=q->next;
       q->next=p->next;
   }
   x=p->exp;
   c=p->coef;
   delete p;
   return *this;
}
template<class T>
void List<T>::PrintList( )
{
  Node<T> *current;
  for(current=first;current;current=current->next)
  {
      if (current->coef>=0&¤t!=first)
      {
        if (0==current->exp)
            cout<<"+"<<current->coef;
        else if (1==current->exp)
            cout<<"+"<<current->coef<<"x";
        else
            cout<<"+"<<current->coef<<"x^"<<current->exp;
      }
      else
      {
        if (0==current->exp)
            cout<<current->coef;
        else if (1==current->exp)
            cout<<current->coef<<"x";
        else
            cout<<current->coef<<"x^"<<current->exp;
      }
  }
  cout<<endl;
}
template<class T>
List<T>& List<T>::polyAdd(List<T>& poly2)
{
    Node<T> *p=first,*q=poly2.first,*before=first;
    while(q!=0)
    {
        if (p!=0)
        {
            if (p->exp<q->exp)
            {
                before=p;
                p=p->next;
            }
            else if (p->exp>q->exp) 
            {
                Insert(Locate(p->coef,p->exp),q->coef,q->exp);
                q=q->next;
            }
            else if (p->exp==q->exp)
            {
                p->coef+=q->coef;
                before=p;
                p=p->next;
                q=q->next;
            }
        }
        else
        {
            Insert(Length(),q->coef,q->exp);
            q=q->next;
        }
    }
    return *this;
}
template <class T>
List<T>& List<T>::polyMul(List<T>& poly2,List<T>& poly3)
{
    Node<T> *p=first,*q=poly2.first;
    int i=0;
    T c,x;
    while(p!=0)
    {
        while(q!=0)
        {
            c=p->coef*q->coef;
            x=p->exp+q->exp;
            q=q->next;
            poly3.Insert(i,c,x);
            i++;
        }
        p=p->next;
        q=poly2.first;
    }
    return *this;
}
template <class T>
List<T>& List<T>::mergerPoly()
{
    Node<T> *p=first,*q=p->next,*beforeQ=first,*temp;
    while(p!=0&&p->next!=0)
    {
        while(q!=0)
        {
            if (p->exp==q->exp)
            {
                p->coef+=q->coef;
                temp=q->next;
                delete q;
                q=temp;
                beforeQ->next=q;
            }
            else
            {
                beforeQ=q;
                q=q->next;
            }
        }
        p=p->next;
        beforeQ=p;
        if (beforeQ!=0)    q=p->next;
    }
    Node<T> *beforeP=0;
    p=first;
    while(p!=0)
    {
        if (0==p->coef)
        {    
            temp=p->next;
            delete p;
            p=temp;
            if (beforeP!=0)    beforeP->next=p;
            else first=p;
        }
        else
        {
            beforeP=p;
            p=p->next;
        }
    }
    return *this;
}
int main()
{
    List<int> poly1,poly2,poly3;
    int c,x;
    cout <<"输入第一个多项式,提示:输入0 0,结束多项式输入。"<<endl;
    while(0!=c||0!=x)
    {
        cout <<"请输入多项式的系数:" ;
        cin >>c;
        cout <<"请输入多项式的指数:" ;
        cin >>x;
        if (c!=0||x!=0)
            poly1.Insert(poly1.Length(),c,x);
    }
    c=1;
    cout <<"输入第二个多项式,提示:输入0 0,结束多项式输入。"<<endl;
    while(0!=c||0!=x)
    {
        cout <<"请输入多项式的系数:" ;
        cin >>c;
        cout <<"请输入多项式的指数:" ;
        cin >>x;
        if (c!=0||x!=0)
            poly2.Insert(poly2.Length(),c,x);
    }
    cout<<"A(x)=";
    poly1.PrintList();
    cout<<"B(x)=";
    poly2.PrintList();
    cout <<"请输入要做的运算,1加法,2乘法:";
    cin >>c;
    if (1==c)
    {
        poly1.polyAdd(poly2);
        poly1.mergerPoly();
        cout <<"A(x)+B(x)=";
        poly1.PrintList();
    }
    else if(2==c)
    {
        poly1.polyMul(poly2,poly3);
        poly3.mergerPoly();
        cout <<"A(x)*B(x)=";
        poly3.PrintList();
    }
    else cout<<"输入错误";
    
    return 0;
}

你可能感兴趣的:(C++算法与数据结构学习笔记------单链表实现多项式)