• 两个多项式相加运算

    两个多项式相加运算

    1. #include <stdio.h>
    2. #include <malloc.h>
    3. typedef struct node
    4. { float coef; /*序数*/
    5. int expn; /*指数*/
    6. struct node *next; /*指向下一个结点的指针*/
    7. } PolyNode;
    8. void InitList(PolyNode *&L) /*初始化多项式单链表*/
    9. {
    10. L=(PolyNode *)malloc(sizeof(PolyNode)); /*建立头结点*/
    11. L->next=NULL;
    12. }
    13. int GetLength(PolyNode *L) /*求多项式单链表的长度*/
    14. {
    15. int i=0;
    16. PolyNode *p=L->next;
    17. while (p!=NULL) /*扫描单链表L,用i累计数据结点个数*/
    18. {
    19. i++;p=p->next;
    20. }
    21. return i;
    22. }
    23. PolyNode *GetElem(PolyNode *L,int i) /*返回多项式单链表中第i个结点的指针*/
    24. {
    25. int j=1;
    26. PolyNode *p=L->next;
    27. if (i<1 || i>GetLength(L))
    28. return NULL;
    29. while (j<i) /*沿next域找第i个结点*/
    30. {
    31. p=p->next;j++;
    32. }
    33. return p;
    34. }
    35. PolyNode *Locate(PolyNode *L,float c,int e) /*在多项式单链表中按值查找*/
    36. {
    37. PolyNode *p=L->next;
    38. while (p!=NULL && (p->coef!=c ||p->expn!=e))
    39. p=p->next;
    40. return p;
    41. }
    42. int InsElem(PolyNode *&L,float c,int e,int i) /*在多项式单链表中插入一个结点*/
    43. {
    44. int j=1;
    45. PolyNode *p=L,*s;
    46. s=(PolyNode *)malloc(sizeof(PolyNode));
    47. s->coef=c;s->expn=e;s->next=NULL;
    48. if (i<1 || i>GetLength(L)+1)
    49. return 0;
    50. while (j<i) /*查找第i-1个结点*p*/
    51. {
    52. p=p->next;j++;
    53. }
    54. s->next=p->next;
    55. p->next=s;
    56. return 1;
    57. }
    58. int DelElem(PolyNode *L,int i) /*在多项式单链表中删除一个结点*/
    59. {
    60. int j=1;
    61. PolyNode *p=L,*q;
    62. if (i<1 || i>GetLength(L))
    63. return 0;
    64. while (j<i) /*在单链表中查找第i-1个结点*p*/
    65. {
    66. p=p->next;j++;
    67. }
    68. q=p->next;
    69. p->next=q->next;
    70. free(q);
    71. return 1;
    72. }
    73. void DispList(PolyNode *L) /*输出多项式单链表的元素值*/
    74. {
    75. PolyNode *p=L->next;
    76. while (p!=NULL)
    77. {
    78. printf("(%g,%d) ",p->coef,p->expn);
    79. p=p->next;
    80. }
    81. printf("\n");
    82. }
    83. void CreaPolyList(PolyNode *&L,float C[],int E[],int n)
    84. {
    85. int i;
    86. InitList(L);
    87. for (i=0;i<n;i++)
    88. InsElem(L,C[i],E[i],i+1);
    89. }
    90. void SortPloy(PolyNode *&L) /*对L的多项式单链表按expn域递增排序*/
    91. {
    92. PolyNode *p=L->next,*q,*pre;
    93. L->next=NULL;
    94. while (p!=NULL)
    95. {
    96. if (L->next==NULL) /*处理第1个结点*/
    97. {
    98. L->next=p;p=p->next;
    99. L->next->next=NULL;
    100. }
    101. else /*处理其余结点*/
    102. {
    103. pre=L;q=pre->next;
    104. while (q!=NULL && p->expn>q->expn) /*找q->expn刚大于或等于p->expn的结点*q的前驱结点*pre*/
    105. {
    106. pre=q;q=q->next;
    107. }
    108. q=p->next; /*在*pre结点之后插入*p*/
    109. p->next=pre->next;
    110. pre->next=p;
    111. p=q;
    112. }
    113. }
    114. }
    115. PolyNode *AddPoly(PolyNode *pa,PolyNode *pb)
    116. {
    117. PolyNode *pc,*p1=pa->next,*p2=pb->next,*p,*tc,*s;
    118. pc=(PolyNode *)malloc(sizeof(PolyNode)); /*新建头结点*/
    119. pc->next=NULL; /*pc为新建单链表的头结点*/
    120. tc=pc; /*tc始终指向新建单链表的最后结点*/
    121. while (p1!=NULL && p2!=NULL)
    122. {
    123. if (p1->expn<p2->expn) /*将*p1结点复制到*s并链到pc尾*/
    124. {
    125. s=(PolyNode *)malloc(sizeof(PolyNode));
    126. s->coef=p1->coef;s->expn=p1->expn;s->next=NULL;
    127. tc->next=s;tc=s;
    128. p1=p1->next;
    129. }
    130. else if (p1->expn>p2->expn) /*将*p2结点复制到*s并链到pc尾*/
    131. {
    132. s=(PolyNode *)malloc(sizeof(PolyNode));
    133. s->coef=p2->coef;s->expn=p2->expn;s->next=NULL;
    134. tc->next=s;tc=s;
    135. p2=p2->next;
    136. }
    137. else /*p1->expn=p2->expn的情况*/
    138. {
    139. if (p1->coef+p2->coef!=0) /*序数相加不为0时新建结点*s并链到pc尾*/
    140. {
    141. s=(PolyNode *)malloc(sizeof(PolyNode));
    142. s->coef=p1->coef+p2->coef;s->expn=p1->expn;
    143. s->next=NULL;
    144. tc->next=s;tc=s;
    145. }
    146. p1=p1->next;p2=p2->next;
    147. }
    148. }
    149. if (p1!=NULL) p=p1; /*将尚未扫描完的余下结点复制并链接到pc单链表之后*/
    150. else p=p2;
    151. while (p!=NULL)
    152. {
    153. s=(PolyNode *)malloc(sizeof(PolyNode));
    154. s->coef=p->coef;s->expn=p->expn;s->next=NULL;
    155. tc->next=s;tc=s;
    156. p=p->next;
    157. }
    158. tc->next=NULL; /*新建单链表最后结点的next域置空*/
    159. return pc;
    160. }
    161. void main()
    162. {
    163. PolyNode *L1,*L2,*L3;
    164. float C1[]={3,7,5,9},C2[]={-9,8,22};
    165. int E1[]={1,0,17,8},E2[]={8,1,7};
    166. InitList(L1);
    167. InitList(L2);
    168. InitList(L3);
    169. CreaPolyList(L1,C1,E1,4);
    170. CreaPolyList(L2,C2,E2,3);
    171. printf("两多项式相加运算\n");
    172. printf(" 原多项式A:");DispList(L1);
    173. printf(" 原多项式B:");DispList(L2);
    174. SortPloy(L1);
    175. SortPloy(L2);
    176. printf("排序后的多项式A:");DispList(L1);
    177. printf("排序后的多项式B:");DispList(L2);
    178. L3=AddPoly(L1,L2);
    179. printf("多项式相加结果:");DispList(L3);
    180. }