码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构:2.5 链表例子(一元多项式乘法与加法)

时间:2021-02-06 11:57:04      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:type   while   system   pause   malloc   nbsp   attach   main   clu   

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct PolyNode *Polynomial;
  5 struct PolyNode {
  6     int coef;
  7     int expon;
  8     Polynomial link;
  9 };
 10 
 11 void Attach ( int c, int e, Polynomial *pRear ){
 12     Polynomial P;
 13 
 14     P = (Polynomial)malloc(sizeof( struct PolyNode ));
 15     P->coef = c;  //指向系数
 16     P->expon = e;  //指向指数
 17     P->link = NULL;
 18     (*pRear)->link = P;
 19     *pRear = P;
 20 }
 21 
 22 Polynomial ReadPoly() {
 23     int N,c,e;
 24     Polynomial P,Rear,t;
 25 
 26     scanf("%d",&N);
 27     P = (Polynomial)malloc(sizeof(struct PolyNode));
 28     P->link = NULL;
 29     Rear = P;
 30     while(N--) {
 31         scanf( "%d %d", &c,&e );
 32         Attach(c,e,&Rear);
 33     }
 34     t = P; P = P->link; free(t);
 35     return P;
 36 }
 37 
 38 Polynomial Add( Polynomial P1,Polynomial P2 ) {
 39     Polynomial t1, t2, P, Rear,temp;
 40     int sum;
 41 
 42     t1 = P1; t2 = P2;
 43     P = (Polynomial)malloc(sizeof(struct PolyNode));
 44     P->link = NULL;
 45     Rear = P; 
 46     while(t1&&t2) {
 47         if (t1->expon == t2->expon) {   
 48             sum = t1->coef + t2->coef;
 49             if(sum) Attach(sum,t1->expon,&Rear);
 50             t1 = t1->link;
 51             t2 = t2->link;
 52         }
 53         else if (t1->expon > t2->expon) {
 54             Attach(t1->coef,t1->expon,&Rear);
 55             t1 = t1->link;
 56         }
 57         else  {
 58             Attach(t2->coef,t2->expon,&Rear);
 59             t2 = t2->link;
 60         }      
 61     }
 62     while(t1) { Attach(t1->coef,t1->expon,&Rear); t1 = t1->link; }
 63     while(t2) { Attach(t2->coef,t2->expon,&Rear); t2 = t2->link; }
 64     temp = P; P = P->link; free(temp);
 65     return P;
 66  } 
 67 
 68  Polynomial Mult(Polynomial P1,Polynomial P2) {
 69     Polynomial P,Rear,t1,t2,t;
 70     int c,e;
 71 
 72     if(!P1||!P2) return NULL;
 73 
 74     t1 = P1; t2 = P2;
 75     P = (Polynomial)malloc(sizeof(struct PolyNode));
 76     Rear = P;
 77     while(t2) {
 78         Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
 79         t2 = t2->link;
 80     }
 81     t1 = t1->link;
 82     while(t1) {
 83         t2 = P2; Rear = P;
 84         while(t2) {
 85             c = t1->coef*t2->coef;
 86             e = t1->expon+t2->expon;
 87             while(Rear->link&&Rear->link->expon>e) {
 88                 Rear = Rear->link;
 89             }
 90             if(Rear->link&&Rear->link->expon==e){
 91                 if(Rear->link->coef+c){
 92                     Rear->link->coef += c;
 93                 }
 94                 else{
 95                     t = Rear->link;
 96                     Rear->link = t->link;
 97                     free(t);
 98                 }
 99             }
100             else{
101                 t = (Polynomial)malloc(sizeof(struct PolyNode));
102                 t->expon = e; t->coef = c; t->link = Rear->link;
103                 Rear->link = t;
104                 Rear = Rear->link;
105             }
106             t2 = t2->link;
107         }
108         t1 = t1->link;
109     }
110     t2 = P; P = P->link; free(t2);
111 
112     return P;
113 }
114 
115 void PrintPoly(Polynomial P) {
116     int flag = 0;
117 
118     if(!P) {printf("0 0\n"); return;}
119 
120     while(P) {
121         if(!flag)
122             flag = 1;
123         else
124             printf(" ");
125         printf("%d %d",P->coef,P->expon);
126         P = P->link;
127     }
128     printf("\n");
129 }
130 
131 int main() {
132     Polynomial P1,P2,PP,PS;
133 
134     P1 = ReadPoly();
135     P2 = ReadPoly();
136     PP = Mult(P1,P2);
137     PrintPoly(PP);
138     PS = Add(P1,P2);
139     PrintPoly(PS);
140 
141     system ("pause");
142 
143     return 0;
144 }

 

数据结构:2.5 链表例子(一元多项式乘法与加法)

标签:type   while   system   pause   malloc   nbsp   attach   main   clu   

原文地址:https://www.cnblogs.com/Pio-GD/p/14379402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!