02-线性结构2 一元多项式的乘法与加法运算

在这里插入图片描述

1.代码

#include<stdio.h>
#include<stdlib.h>
typedef struct PolyNode *Polynomial;
struct PolyNode{
    int coef;   
    int expon;
    Polynomial link;
};


/*构造一个新节点, 插入到当前多项式的后面*/
void Attach(int c, int e, Polynomial *pRear){
    Polynomial P;
    
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    P->link = NULL;
    (*pRear)->link = P;
    *pRear = P;
}

/* 读入多项式 */
Polynomial ReadPoly(){
    Polynomial P, Rear, t;   /*Rear 指向当前结果多项式的最后一项*/
    int c, e, N;
    
    scanf("%d", &N);
    P = (Polynomial)malloc(sizeof(struct PolyNode));  /* 链表头空结点*/
    P->link = NULL;
    Rear = P;     
    while(N--){
        scanf("%d %d", &c, &e);
        Attach(c, e, &Rear);     /* 将当前项插入多项式尾部,且由于在Attach函数中 Rear的指向要改变,所以借助指针保证 */
    }
    t = P; 
    P = P->link; 
    free(t);
    return P;
}


Polynomial Add(Polynomial P1, Polynomial P2){
    Polynomial P, Rear, t, t1, t2;
    int sum;
    t1 = P1;
    t2 = P2;
    
    //构造空结点
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    Rear = P;
    
    // t1 t2都不空
    while(t1 && t2){
        if(t1->expon == t2->expon){
            sum = t1->coef + t2->coef;
            if(sum) 
                Attach(sum, t1->expon, &Rear);
            t1 = t1->link;
            t2 = t2->link;
        }else if(t1->expon > t2->expon){
            Attach(t1->coef, t1->expon, &Rear);
            t1 = t1->link;
        }else{
            Attach(t2->coef, t2->expon, &Rear);
            t2 = t2->link;
        }
    }
    // 将未处理的另一个多项式复制到
    while(t1){
        Attach(t1->coef, t1->expon, &Rear);
        t1 = t1->link;
    }
    while(t2){
        Attach(t2->coef, t2->expon, &Rear);
        t2 = t2->link;
    }
    Rear->link = NULL;
    t = P;
    P = P->link;
    free(t);
    return P;
}


Polynomial Mult(Polynomial P1, Polynomial P2){
    Polynomial P, Rear, t, t1, t2;
    int c, e;
    
    if(!P1 || !P2)
        return NULL;
    
    t1 = P1;
    t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    Rear = P;
    
    while(t2){     /* 先用P1 的第一项乘以P2, 得到P*/
        Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
        t2 = t2->link;
    }
    t1 = t1->link;
    while(t1){
        t2 = P2;
        Rear = P;
        while(t2){
            e = t1->expon + t2->expon;
            c = t1->coef * t2->coef;
            
            //寻找结点插入的位置 —— Rear->link
            while(Rear->link && Rear->link->expon > e)
                Rear = Rear->link;
            if(Rear->link && Rear->link->expon == e){
                if(Rear->link->coef + c)
                    Rear->link->coef += c;
                else{     /* 系数为 0, 删除*/
                    t = Rear->link;
                    Rear->link = t->link;
                    free(t);
                }
            }else{
                 t = (Polynomial)malloc(sizeof(struct PolyNode));
                 t->coef = c;
                 t->expon = e;
                 t->link = Rear->link;
                 Rear->link = t;    /
                 Rear = Rear->link;
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    t2 = P;
    P = P->link;
    free(t2);
    return P;
}

void PrintPoly(Polynomial P){
    int flag = 0;
    
    if(!P){
        printf("0 0\n");
        return;
    }
    while(P){
        if(!flag)
            flag = 1;
        else
            printf(" ");
        printf("%d %d", P->coef, P->expon);
        P = P->link;
    }
    printf("\n");
}

int main(){
    Polynomial P1, P2, PP, PS;
    
    P1 = ReadPoly();
    P2 = ReadPoly();
    PP = Mult(P1, P2);
    PrintPoly(PP);
    PS = Add(P1, P2);
    PrintPoly(PS);
    
    return 0;
}

2.小结

  • 当某一变量要在函数调用中改变时,应该设定函数参数是指针类型的
 Attach(c, e, &Rear);    
  /* 将当前项插入多项式尾部,且由于在Attach函数中 Rear的指向要改变,所以借助指针保证 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值