线性表的基本运算及多项式的算术运算

线性表的基本运算:

template

voidSeqList::Reverse(){

  T* t= new T[maxLength];

  int j=0;

  for (int i = 0; i < n; i++)  //利用中间数组逆置

  {

    t[i] = elements[i];

  }

  for (int a = n; a>=0; a--)

  {

    elements[j++] = t[a-1];

  }

}

template

bool SeqList::DeleteX(constT &x){

int flag;

for(int i=0;i

     if(elements[i]==x){

        flag = 1;

        Delete(i);

     }

}

if(flag)

     return true;

return false;

}


多项式的算术运算:

Polynomal operator*(Polynomal& A,Polynomal&B){

       Term*pa,*pb,*pc;

       intAL,BL,i,k,maxExp;

       PolynomalC;            //存储结果多项式

       pc=C.getHead();         //结果多项式的尾指针

       AL=A.maxOrder();

       BL=B.maxOrder();

       if(AL!=-1||BL!=-1)        //相乘运算

       {

              maxExp=AL+BL;          //结果最高项

              float*result=new float[maxExp+1];

              for(i = 0; i < maxExp; i++)

              {

                     result[i]=0.0;

              }                   

              pa=A.getHead()->link;//多项式A遍历指针

              while(pa!=NULL){  

                     pb=B.getHead()->link;  //多项式B遍历指针

                     while(pb!=NULL){

                            k=pa->exp+pb->exp;

                            result[k]=result[k]+pa->coef*pb->coef;

                            pb=pb->link;

                     }

                     pa=pa->link;

              }

              for(i = 0; i <= maxExp; i++)

                     if(fabs(result[i])>0.001)

                            pc=pc->InsertAfter(result[i],i);

                     delete[]result;

       }

       pc->link=NULL;

       return C;

}

你可能感兴趣的:(南邮_数据结构上机实验)