多项式

struct PolyNode {
    int coef; //系数
    int expon; //指数
    struct PolyNode *link; //指向下一个节点的指针
} *Polynomial;

Polynomial PolyAdd(Polynomial P1, Polynomial P2) {
    Polynomial front, rear, temp;
    int sum;
    rear = (Polynomial)malloc(sizeof(struct PolyNode));
        front  = rear; //由front记录多项式链表头结点
    while(P1&&P2) { //当两个多项式都有非零项待处理时
        switch(Compare(P1->expon, P2->expon)) {
            case 1:
                Attach(P1->coef, P1->expon, &rear);
                P1 = P1->link;
                break;
            case -1:
                Attach(P2->coef, P2->expon, &rear);
                P2 = P2->link;
                break;
            case 0:
                sum = P1->coef + P2->coef;
                if (sum) {
                    Attach(sum, P1->expon, &rear);
                }
                P1 = P1->link;
                P2 = P2->link;
                break;
        }
        for(;P1;P1 = P1->link) { //将未处理完的另一个多项式的所有结点一次复制到结果多项式中去
            Attach(P1->coef, P1->expon, &rear);
        }
        for(;P2;P2 = P2->link) { 
            Attach(P2->coef, P2->expon, &rear);
        }
        rear->link = NULL;
        temp = front;
        front = front->link; //令front指向结果多项式第一个非零项
        free(temp); //释放临时空表头结点
        return front;
    }
}

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; //修改pRear的值
}

你可能感兴趣的:(多项式)