• 多项式ADT加法乘法——单链表实现


      1 /*
      2   多项式ADT——单链表实现
      3 */
      4 
      5 /*接口头文件*/
      6 #include <stdbool.h>
      7 typedef int ElementType;
      8 
      9 #ifndef _POLYNOMIAL_H
     10 #define _POLYNOMIAL_H
     11 
     12 struct Node;
     13 typedef struct Node * PtrToNode;
     14 typedef PtrToNode Polynomial;
     15 typedef PtrToNode Position;
     16 
     17 /*操作集*/
     18 Polynomial MakeEmpty( Polynomial Poly );
     19 bool IsEmpty( Polynomial Poly );
     20 bool IsLast( Position P );
     21 Position First( Polynomial Poly );
     22 Position Header( Polynomial Poly );void DeleteList( Polynomial Poly );
     23 void Insert( ElementType Coeff, ElementType Expon, Position P );
     24 Position Advance( Position P );void PrintfList( Polynomial Poly );
     25 void ListInsertSort( Polynomial Head );
     26 Polynomial AddPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomial PolySum );
     27 Polynomial MulPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomial PolyProd );
     28 
     29 #endif
     30 
     31 /*接口实现*/ 
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include "polynomial.h"
     35 
     36 /*特定结构声明*/
     37 struct Node
     38 {
     39     ElementType Coefficient;
     40     ElementType Exponent;
     41     Position Next;
     42 };
     43 
     44 /*合并同类项*/
     45 Polynomial CombSimPolynomial( Polynomial Poly ); 
     46 
     47 Polynomial MakeEmpty( Polynomial Poly )
     48 {
     49     if ( Poly != NULL )
     50        DeleteList( Poly );
     51        
     52     Poly = ( Polynomial )malloc( sizeof( struct Node ) );
     53     if (Poly == NULL )
     54     {
     55        printf( "No Space,quit!
    " );
     56        exit( 1 );
     57     }
     58     Poly->Next = NULL;
     59     
     60     return Poly;
     61 }
     62 
     63 bool IsEmpty( Polynomial Poly )
     64 {
     65     return Poly->Next == NULL;
     66 }
     67 
     68 bool IsLast( Position P )
     69 {
     70     return P->Next == NULL;
     71 }
     72 
     73 Position First( Polynomial Poly )
     74 {
     75     return Poly->Next;
     76 }
     77 
     78 Position Header( Polynomial Poly )
     79 {
     80     return Poly;
     81 }
     82 
     83 void DeleteList( Polynomial Poly )
     84 {
     85     Position P;
     86     Position Temp;
     87     
     88     P = First( Poly );
     89     while ( P != NULL )
     90     {
     91         Temp = Advance( P );
     92         free( P );
     93         P = Temp;
     94     }
     95 }
     96 
     97 void Insert( ElementType Coeff, ElementType Expon, Position P )
     98 {
     99     Position Temp;
    100     
    101     Temp = ( Polynomial )malloc( sizeof ( struct Node ) );
    102     if ( Temp == NULL )
    103     {
    104        printf( "No Space,quit!
    " );
    105        exit( 1 );
    106     }
    107     Temp->Coefficient = Coeff;
    108     Temp->Exponent = Expon;
    109     Temp->Next = P->Next;
    110     
    111     P->Next = Temp;
    112 }
    113 
    114 Position Advance( Position P )
    115 {
    116     return P->Next;
    117 }
    118 
    119 void PrintfList( Polynomial Poly )
    120 {
    121     Position P;
    122     
    123     if ( IsEmpty( Poly ) )
    124        printf( "No Data
    " );
    125     else
    126     {
    127        P = First( Poly );
    128        while ( P != NULL )
    129        {
    130             /*当系数为0时,不输出*/ 
    131             if ( P->Coefficient != 0)
    132             {
    133                 /*指数为0时,只显示常数*/ 
    134                 if ( P->Exponent == 0 )
    135                   printf( "%d", P->Coefficient );
    136                 /*当位置P不是尾部且下个P不为负数,结果显示+号*/  
    137                 else if ( P->Next != NULL && P->Next->Coefficient > 0 )
    138                    printf( "%d^%d+", P->Coefficient, P->Exponent );
    139                  /*当位置P是尾部或下个P为负数,结果不显示+号*/  
    140                 else
    141                    printf( "%d^%d", P->Coefficient, P->Exponent );
    142             }
    143             P = Advance( P );
    144        }
    145        printf( "
    " );
    146     }
    147 }
    148 
    149 /*
    150    链表插入排序
    151    策略:
    152              将原链表Head拆分链表Head和链表Head1。其中链表Head只有一个元素。
    153              不断从Head1中拿出一个元素插入Head中,直到Head1中没有元素为止。
    154 */
    155 void ListInsertSort( Polynomial Head )
    156 {
    157     Polynomial Head1;
    158     Position P;
    159     Position Q;
    160     Position Temp;
    161 
    162     /*空表或者只有一个元素则不排序*/
    163     if ( Head->Next == NULL || Head->Next->Next == NULL )
    164        return;
    165     else
    166     {
    167        /*第一次拆分*/
    168        Head1 = Head->Next->Next;
    169        Head->Next->Next = NULL;
    170     
    171        while ( Head1 != NULL )
    172        {
    173            /*降序排列*/
    174            for ( P = First( Head ), Q = Header( Head ); P != NULL && Head1->Exponent < P->Exponent; Q = P, P = P->Next) 
    175                  continue;
    176             
    177             Temp = Head1;                 //待插入元素
    178             Head1 = Head1->Next;  
    179             /*插入元素*/
    180             Q->Next = Temp;
    181             Temp->Next = P;
    182        }
    183        
    184     }
    185 }
    186 
    187 /*类似链表并集,核心是归并操作*/
    188 Polynomial AddPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomial PolySum )
    189 {
    190     Position P;
    191     Position Ptr1;
    192     Position Ptr2;
    193     
    194     Ptr1 = First( Poly1 );
    195     Ptr2 = First( Poly2 );
    196     PolySum = MakeEmpty( NULL );
    197     P = Header( PolySum );
    198     while ( Ptr1 != NULL && Ptr2 != NULL )
    199     {
    200         if ( Ptr1->Exponent  < Ptr2->Exponent )
    201         {
    202            Insert( Ptr2->Coefficient, Ptr2->Exponent, P );
    203            Ptr2 = Advance( Ptr2 );
    204         }
    205         else if ( Ptr1->Exponent > Ptr2->Exponent )
    206         {
    207            Insert( Ptr1->Coefficient, Ptr1->Exponent, P );
    208            Ptr1 = Advance( Ptr1 );
    209         }
    210         else
    211         {
    212            Insert( Ptr1->Coefficient + Ptr2->Coefficient, Ptr1->Exponent, P );
    213            Ptr1 = Advance( Ptr1 );
    214            Ptr2 = Advance( Ptr2 );
    215         }
    216         P = Advance( P );
    217     }
    218     
    219     while ( Ptr1 != NULL )
    220     {
    221         Insert( Ptr1->Coefficient, Ptr1->Exponent, P );
    222         P = Advance( P );
    223         Ptr1 = Advance( Ptr1 );
    224     }
    225     
    226     while ( Ptr2 != NULL )
    227     {
    228         Insert( Ptr2->Coefficient, Ptr2->Exponent, P );
    229         P = Advance( P );
    230         Ptr2 = Advance( Ptr2 );
    231     }
    232     
    233     return PolySum;
    234 }
    235 
    236 Polynomial MulPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomial PolyProd )
    237 {
    238     Position P;
    239     Position Ptr1;
    240     Position Ptr2;
    241     
    242     PolyProd = MakeEmpty( NULL );
    243     P = Header( PolyProd );
    244     Ptr1 = First( Poly1 );
    245     Ptr2 = First( Poly2 );
    246     
    247     while ( Ptr1 != NULL )
    248     {
    249         while ( Ptr2 != NULL )
    250         {
    251             Insert( ( Ptr1->Coefficient ) * ( Ptr2->Coefficient ), Ptr1->Exponent + Ptr2->Exponent, P );
    252             P = Advance( P );
    253             Ptr2 = Advance( Ptr2 ); 
    254         }
    255         Ptr1 = Advance( Ptr1 );
    256         Ptr2 = First( Poly2 );
    257     }
    258     /*结果排序*/
    259     ListInsertSort( PolyProd );
    260     /*合并同类项*/
    261     PolyProd = CombSimPolynomial( PolyProd );
    262     
    263     return PolyProd;  
    264 }
    265 
    266 Polynomial CombSimPolynomial( Polynomial Poly )
    267 {
    268     Polynomial Result;
    269     Position P;
    270     Position Q;
    271     
    272     
    273     if ( Poly == NULL || Poly->Next->Next == NULL )
    274        return Poly;
    275     else
    276     {
    277        P = Poly->Next;
    278        Q = P->Next;
    279        while ( Q != NULL )
    280        {
    281            if ( P->Exponent == Q->Exponent )
    282            {
    283               P->Coefficient += Q->Coefficient;
    284               P->Next = Q->Next;
    285               Q = Q->Next;
    286            }
    287            else
    288            {
    289               P = P->Next;
    290               Q = Q->Next;
    291            }
    292        }
    293        return Poly;
    294     }
    295 }
  • 相关阅读:
    圣杯布局,不太明白为什么后面的元素会飘上来
    CSS实现宽高成比例缩放
    javascript原生ajax;
    http-关于application/x-www-form-urlencoded等字符编码的解释说明
    jQuery判断滚动条滚到页面底部脚本
    下拉顶部刷新简单实现
    swig模板下拉框应用
    swig模板中文资料
    张宵 20200924-2 功能测试
    张宵 20200924-5 四则运算试题生成
  • 原文地址:https://www.cnblogs.com/weixia-blog/p/7307375.html
Copyright © 2020-2023  润新知