链表_多项式相加相乘

加法部分运行成功。乘法仍存在问题,找机会解决,欢迎大家指正。

还有一个问题,C语言函数传地址如何传,是否不需要我这样多次申请内存空间?欢迎交流。

代码如下:

  1 #include<stdio.h>
  2 #include<malloc.h> 
  3 
  4 typedef struct PolyNode *Poly;//定义多项式结构体类型 
  5 struct PolyNode{
  6     int coef;
  7     int expon;
  8     struct PolyNode *next;
  9 };
 10 
 11 void attach(int c,int e,Poly *p);
 12 Poly ReadPoly();
 13 Poly Add(Poly p1,Poly p2);
 14 Poly Mult(Poly p1,Poly p2);
 15 void PrintPoly(Poly p);
 16 
 17 int main()
 18 {
 19     Poly p1,p2,pm,pa;//读 读 乘 加 
 20     p1=ReadPoly();
 21     p2=ReadPoly();
 22     PrintPoly(p1);
 23     PrintPoly(p2);
 24     pm=Mult(p1,p2);
 25     pa=Add(p1,p2);
 26     PrintPoly(pm);
 27     PrintPoly(pa);
 28     
 29     return 0;
 30  } 
 31  
 32  /*读一个多项式*/ 
 33  Poly ReadPoly()
 34  {
 35      int N,c,e;//项数,系数,指数 
 36      Poly p,rear,t;//链表地址、每次的尾,临时t 
 37     scanf("%d",&N);//输入几项 
 38     
 39     p=(Poly)malloc(sizeof(struct PolyNode));//申请一个空的头结点 
 40     p->next=NULL;
 41     rear=p;
 42     
 43     while(N--){//一项一项链在后面 
 44         scanf("%d %d",&c,&e);
 45         attach(c,e,&rear);
 46     }
 47     
 48     t=p;//删去空的头结点 
 49     p=p->next;
 50     free(t);
 51 
 52     return p;//返回p 
 53  }
 54 
 55  /*把一个新节点连在原先链上*/ 
 56  void attach(int c,int e,Poly *p)
 57  {
 58      Poly addnode;
 59      addnode=(Poly)malloc(sizeof(struct PolyNode));//malloc 一个新节点 把数存进去 
 60     addnode->coef=c;
 61      addnode->expon=e;
 62      addnode->next=NULL;
 63      (*p)->next=addnode;//把新节点连在链上 
 64      *p=addnode;
 65  }
 66  
 67 
 68   /*多项式相加*/ 
 69  Poly Add(Poly p1,Poly p2)
 70  {
 71      Poly p,t1,t2,rear,temp;//新链 相加的链1、2 新链尾 临时指向头结点 
 72      t1=p1;
 73      t2=p2;
 74      p=(Poly)malloc(sizeof(struct PolyNode));//动态申请新链 即一个空节点 
 75      p->next=NULL;
 76      rear=p;
 77      
 78      while(t1&&t2){// 当t1、t2都没加完 
 79          if(t1->expon==t2->expon){//如果指数相等 
 80              if((t1->coef+t2->coef)!=0){//如果系数和不为零 
 81              attach(t1->coef+t2->coef,t1->expon,&rear);//往新链表加一个节点 
 82              }
 83             t1=t1->next;//t1、t2都后移 
 84             t2=t2->next;
 85          } 
 86         else if(t1->expon>t2->expon){//如果当前t1指的项比当前t2指的项指数大 往新的链表加t1当前这个节点 
 87              attach(t1->coef,t1->expon,&rear);
 88              t1=t1->next; 
 89          }
 90          else {
 91          attach(t2->coef,t2->expon,&rear);
 92          t2=t2->next;
 93          }
 94      }
 95      for(;t1;t1=t1->next)attach(t1->coef,t1->expon,&rear);//把剩下的一个链的剩下的全部接到新链表 
 96      for(;t2;t2=t2->next)attach(t2->coef,t2->expon,&rear);
 97      rear->next=NULL;//最后一个节点的设置 
 98      temp=p;//删第一个空节点 
 99      p=p->next;
100      free(temp);
101      return p;//返回新链表 即和链表 
102  }
103  
104  /*多项式相乘*/ 
105  Poly Mult(Poly p1,Poly p2)
106  {
107     Poly t1,t2,p,rear,temp,t,delhead;
108     int c,e;
109     t1=p1,t2=p2;
110     p=(Poly)malloc(sizeof(struct PolyNode));//创建新链表 
111     rear=p;
112     
113     if(!p1||!p2)return NULL;//若两个链表有一个为空 则根据多项式惩罚意义 新链表为空 
114     
115     while(t2){//用第一个多项式第一项 乘 第二个多项式每一项 存在新链表中 
116         attach(t1->coef*t2->coef,t1->expon+t2->expon,&rear);
117         t2=t2->next;
118     }
119     t1=t1->next;
120         
121     /*第一个多项式从第二项开始每一项 乘 第二个多项式每一项,并添在新链表中 */ 
122     while(t1){
123         while(t2){ 
124             c=t1->coef*t2->coef;//算当前 一个项乘一个项 的结果 
125             e=t1->expon+t2->expon;
126             while(rear->next&&(rear->next)->expon>e){//找到在新链表插入的位置 
127                 rear=rear->next;
128             }
129             
130             if((rear->next)->expon==e){//如果新链表中有和 算完要添进来的项 指数一样的项 就合并 
131                 if(rear->next->coef+c!=0){//(系数不为零则合并 ) 
132                     rear->next->coef+=c; 
133                 }
134                 else{// (系数为零则删除新链表中 指数相同项) 
135                     temp=rear;
136                     rear=rear->next;
137                     free(temp);
138                 }
139             }
140             else{// 如果新链表中没有和 算完要填进来的项 指数一样的项 就把该项插入到所确定的插入位置 
141                 t=(Poly)malloc(sizeof(struct PolyNode));
142                 t->coef=c;
143                 t->expon=e;
144                 t->next=rear->next;
145                 rear->next=t;
146                 rear=rear->next; 
147             }
148             
149         }
150     }
151     
152     delhead=p;//把新链表第一个空节点删除 
153     p=p->next;
154     free(delhead);   
155  }
156  
157   
158  /*输出一个多项式*/
159  void PrintPoly(Poly p)
160  {
161      int flag=0;//用于辅助格式 的标签 
162      
163      if(!p){
164          printf("0 0");
165      }
166     
167     while(p){//本质是链表的遍历 
168          if(!flag)flag=1;//输出形式为 第一项两个 空格 第二项两个 空格 、、、最后一项 回车 
169          else printf(" ");
170          
171          printf("%d %d",p->coef,p->expon);
172          p=p->next; 
173      }
174     printf("
");
175   } 
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
View Code
原文地址:https://www.cnblogs.com/coding-gaga/p/4959885.html