链表实现多项式相加

//均已调试通过,编译器为DEV C++

 /多项式相加程序,用链表实现
//系数 coefficient  指数exponent   多项式 multinomial
 #include <stdio.h>
 typedef struct Lnode {
         int coe;
        int exp;
        struct Lnode *next;
        }Lnode,*LinkList;
   //*******************建立两个链表*************************    
int main()
    {
         
          Start:
            printf("Welcome to use!/n");         //????????????????????????????????
            int length1,length2;
           int i,j,sum;
           char ch;
        LinkList  L1=(LinkList)malloc(sizeof(Lnode));    //注意申请链表的方法
         LinkList   L2=(LinkList)malloc(sizeof(Lnode));
       
           printf("please input the A(x) length:/n");
               scanf("%d",&length1);
               printf("please input A(x) coefficient and exponent:/n");
             LinkList p1=L1;
             LinkList pr1=L1;           //不要因循环改变 L1
            for(i=0;i<length1;i++){ 
                   LinkList q1=(LinkList)malloc(sizeof(Lnode));
                   printf("coefficient:");
                    scanf("%d",&q1->coe);
                    printf("exponent:");
                    scanf("%d",&q1->exp);
                    p1->next=q1;
                    p1=q1;
                     q1->next=NULL;                                
                   }
             while(pr1->next){
               printf("%d*X^%d",pr1->next->coe,pr1->next->exp);
               pr1=pr1->next;
               if(pr1->next)
               printf(" + ");
               }
                  
           printf("/nplease input the B(x) length:/n");
                scanf("%d",&length2);
         
          printf("please input B(x) coefficient and exponent:/n");
             LinkList p2=L2;
             LinkList pr2=L2;
             for(j=0;j<length2;j++){
                 LinkList q2=(LinkList)malloc(sizeof(Lnode));
                 printf("coefficient:");
                    scanf("%d",&q2->coe);
                    printf("exponent:");
                    scanf("%d",&q2->exp);
                  p2->next=q2;
                   p2=q2;
                   q2->next=NULL;                
                } 
                while(pr2->next){
               printf("%d*X^%d",pr2->next->coe,pr2->next->exp);
                    pr2=pr2->next;
                    if(pr2->next)
                       printf(" + ");
               }
//******************合并两个多项式***************************
   
           LinkList p=L1;
            LinkList q=L2,temp;
               while(p->next&&q->next)   {
                 if(p->next->exp==q->next->exp)   //注意此处  ,
                     {   
                       sum=p->next->coe+q->next->coe;
                      if(sum)
                          {
                           p->next->coe=sum;
                         LinkList k2=q->next;
                          p=p->next;
                          q=q->next;
                      //   free(k2);       //不能释放结点
                         }
                      
                       else {
                        LinkList k1=p->next;
                        LinkList k2=q->next;
                           p->next=p->next->next;
                          p=p->next;
                          q=q->next;
                          // free(k1);
                           // free(k2);
                          }
            }
              
          else if(p->next->exp > q->next->exp)             
                 {
                    temp=q->next->next;
                   q->next->next=p->next;                        
                   p->next=q->next;
                   q->next=temp;
                  } 
        else  if(p->next->exp < q->next->exp)
               p=p->next; 
      }
         if(!(p->next)&&q->next)
            p->next=q->next;
        if(!(q->next)&&p->next)         
               ;
//***************输出结果************************         
        LinkList L=L1;
      printf("/nSum(x) :");
           while(L->next){
               printf("%d*X^%d",L->next->coe,L->next->exp);
               L=L->next;  // 别忘了向后走
               if(L->next)
                  printf(" + ");
                }
      printf("/n Do you want again:(y or n)/n");
       ch=getch();                  //与getchar()的区别
       printf("%c/n",ch);
         if(ch=='y')
             goto Start;
         else
            printf("Bye,Bye!/n");
         return 0;
       

                             
                                 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值