浙大数据结构 — 第二讲 — 一个简单链表实现 (c语言)

题目如下:

设计函数分别求两个医院多项式的乘积与和

输入样例:

4 3 4 -5 2 6 1 -2 0 (3x4-5x2+6x-2)
3 5 20 -7 4 3 1 (5x20-7x4+3x)

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

代码如下:
#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);		//接入新的节点,并使得指针rear指向最后一个节点
Polynomial ReadPoly();								//读入一个多项式
Polynomial Add(Polynomial P1, Polynomial P2);		//多项式加运算
Polynomial Mult(Polynomial P1, Polynomial P2);		//多项式乘运算
void PrintPoly(Polynomial P);						//输出多项式P

int main(){
    // 读入多项式1
    // 读入多项式2
    // 乘法运算并输出
    // 加法运算并输出
     Polynomial P1, P2, PP, PS;
    printf("Please enter P1: ");
     P1 = ReadPoly();
    printf("Please enter P2: ");
     P2 = ReadPoly();
     PP = Mult(P1, P2);
     printf("the result of mult is:");
     PrintPoly(PP);
     PS = Add(P1, P2);
     printf("the result of add is:");
     PrintPoly(PS);

    return 0;
}

void Attach(int c, int e, Polynomial *pRear){
    Polynomial P;
// *prear 当前结果表达式项尾指针
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    P->link = NULL;
    (*pRear)->link = P;     //修改pRear的值
    *pRear = P;
}

Polynomial ReadPoly(){
    Polynomial P, Rear, t;
    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);                       //将当前项插入多项式尾部
    }
    t = P;                      //删除临时生成的头节点
    P = P->link;
    free(t);
    return P;
}

Polynomial Add(Polynomial P1, Polynomial P2){
    Polynomial t1, t2, P, Rear, temp;
    t1 = P1;
    t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    Rear = P;
    while (t1&&t2)
    {
        if (t1->expon == t2->expon)
        {
           if((t1->coef)+(t2->coef)){
           Attach((t1->coef)+(t2->coef), 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;
    }
    temp = P;
    P = P->link;
    free(temp);
    return P;
}

Polynomial Mult(Polynomial P1, Polynomial P2){      //先用p1的第一项乘p2,然后插入
    Polynomial P, Rear, t1, t2, t;
    int c, e;

    if(!P1 || !P2)  return 0;

    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;  //t1指向p1第二项
    while (t1)      //双重循环, 插入
    {
        t2 = P2;    Rear = P;   //重新赋值, 重新循环

        while (t2)
        {
            c = t1->coef * t2->coef;
            e = t1->expon + t2->expon;
            while (Rear->link && Rear->link->expon > e) {  
                Rear = Rear->expon;             //rear不是最后一项且下一项的系数大于e  则rear后移一项
            }
            if (Rear->link && (Rear->link->expon == e))         //rear不是最后一项 且下一项系数等于e
            {
                if (Rear->link->coef +c)
                {
                    Rear->link->coef += c;
                }
                else
                {
                    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");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值