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

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

   Node *y=new Node;
   y->coef=c;
   y->exp=x;
   if(k){
      y->next=p->next;
      p->next=y;
   }
   else{
      y->next=first;
      first=y;
   }
   return *this;
}
template
List& List::Delete(int k,T& c,T& x)
{
   Node *p=first;
   if(k==1)
       first=first->next;
   else{
       Node *q=first;
       for(int index=1;indexnext;
       if(!q||!q->next) return *this;
       p=q->next;
       q->next=p->next;
   }
   x=p->exp;
   c=p->coef;
   delete p;
   return *this;
}
template
void List::PrintList( )
{
  Node *current;
  for(current=first;current;current=current->next)
  {
      if (current->coef>=0&¤t!=first)
      {
        if (0==current->exp)
            cout<<"+"<coef;
        else if (1==current->exp)
            cout<<"+"<coef<<"x";
        else
            cout<<"+"<coef<<"x^"<exp;
      }
      else
      {
        if (0==current->exp)
            cout<coef;
        else if (1==current->exp)
            cout<coef<<"x";
        else
            cout<coef<<"x^"<exp;
      }
  }
  cout<
List& List::polyAdd(List& poly2)
{
    Node *p=first,*q=poly2.first,*before=first;
    while(q!=0)
    {
        if (p!=0)
        {
            if (p->expexp)
            {
                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 
List& List::polyMul(List& poly2,List& poly3)
{
    Node *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 
List& List::mergerPoly()
{
    Node *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 *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 poly1,poly2,poly3;
    int c,x;
    cout <<"输入第一个多项式,提示:输入0 0,结束多项式输入。"<>c;
        cout <<"请输入多项式的指数:" ;
        cin >>x;
        if (c!=0||x!=0)
            poly1.Insert(poly1.Length(),c,x);
    }
    c=1;
    cout <<"输入第二个多项式,提示:输入0 0,结束多项式输入。"<>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/C++)