一元稀疏多项式(加减法)

这是我们的数据结构的一个实验,用链表实现一元稀疏多项式的加减法,我写的是一个无排序的。

基本思路是创建一个结构体节点,储存每一项的系数(float),指数(int),和一个next指针。输入两个链表A和B后:

1.当指数相等时,系数相加(减法时则相减);相加完的系数若不为0,则存进链表C;

2.若指数不相等,若A的指数小于B的,将A的节点里的系数,指数都存进链表C;

3.反之,则将B的节点里的系数,指数都存进链表C;

4.当有一个链表的后几项多出来时,也将其存进链表C。

然后输出链表C。

注意事项

1.头结点不能变,在操作时应另外申请一个节点指向头节点或首元节点进行操作。

2.在循环中输入每一个数据前,都要申请节点空间。

3.注意指针指向的地点,注意移动指针和赋值的顺序。

4.注意链表输入的结尾,要有输入结束的判断,并且尾指针为空。

5.指针函数最后要返回一个指针节点,函数的类型也是结构体的类型。

 

代码如下,有详细注释:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PolyNode  // 结构体
{
    float coef;  //系数
    int exp;     //指数
    struct PolyNode * next;
}PolyNode;
typedef PolyNode * Polynomial;   // 类型声明
Polynomial  CreateList()    //建表并输入
{
    Polynomial L,p,r;
    r = L = (Polynomial )malloc(sizeof(PolyNode ));   //头节点是L不能移动
    L->next = NULL;
    float x;
    int y;
    scanf("%f %d",&x,&y);
    while(x && y)   //判断输入结束的条件为( x!=0 && y!=0 )
    {
        p = (Polynomial )malloc(sizeof(PolyNode ));
        p->coef = x;
        p->exp = y;
        p->next = NULL;
        r->next = p;
        r = p;
        scanf("%f %d",&x,&y);
        getchar();  //读掉 0 0 之后的回车
    }
    return L;  // 返回输入的链表
}
Polynomial Add(Polynomial A, Polynomial B)  //加法
{
    Polynomial C,S;
    Polynomial pa,pb,pc;
    float x;
    pa = A->next;
    pb = B->next;
    C = (Polynomial )malloc(sizeof(PolyNode ));  // 申请头节点
    pc = C;   // 操作时使用pc指针
    pc->next = NULL;
    while (pa && pb)
    {
        if(pa->exp == pb->exp)  // 指数相等时
        {
            x = pa->coef + pb->coef;
            if (x)     // 相加完的系数不为0时
            {
                S = (Polynomial )malloc(sizeof(PolyNode ));
                S->coef = x;
                S->exp = pa->exp;
                S->next = NULL;
                pc->next = S;
                pc = S;
            }
            pa = pa->next;
            pb = pb->next;
        }
        else
            if(pa->exp < pb->exp)   //指数不相等且A项指数小于B项指数时
            {
                S = (Polynomial )malloc(sizeof(PolyNode ));
                S->coef = pa->coef;
                S->exp = pa->exp;
                S->next = NULL;
                pc->next = S;
                pc = S;
                pa = pa->next;
            }
            else   //指数不相等且A项指数大于B项指数时
            {
                S = (Polynomial )malloc(sizeof(PolyNode ));
                S->coef = pb->coef;
                S->exp = pb->exp;
                S->next = NULL;
                pc->next = S;
                pc = S;
                pb = pb->next;
            }
    }
    while (pa)   //A链表后几项多出来时
    {
        S = (Polynomial )malloc(sizeof(PolyNode ));
        S->coef = pa->coef;
        S->exp = pa->exp;
        S->next = NULL;
        pc->next = S;
        pc = S;
        pa = pa->next;
    }
    while (pb)   //B链表后几项多出来时
    {
        S = (Polynomial )malloc(sizeof(PolyNode ));
        S->coef = pb->coef;
        S->exp = pb->exp;
        S->next = NULL;
        pc->next = S;
        pc = S;
        pb = pb->next;
    }
    return C;
}
Polynomial Minus(Polynomial A, Polynomial B)  //减法,判断条件等都同上
{
    Polynomial C,S;
    Polynomial pa,pb,pc;
    float x;
    pa = A->next;
    pb = B->next;
    C = (Polynomial )malloc(sizeof(PolyNode ));
    pc = C;
    pc->next = NULL;
    while (pa && pb)
    {
        if(pa->exp == pb->exp)
        {
            x = pa->coef - pb->coef; //系数相减
            if (x)
            {
                S = (Polynomial )malloc(sizeof(PolyNode ));
                S->coef = x;
                S->exp = pa->exp;
                S->next = NULL;
                pc->next = S;
                pc = S;
            }
            pa = pa->next;
            pb = pb->next;
        }
        else
            if(pa->exp < pb->exp)
            {
                S = (Polynomial )malloc(sizeof(PolyNode ));
                S->coef = pa->coef;
                S->exp = pa->exp;
                S->next = NULL;
                pc->next = S;
                pc = S;
                pa = pa->next;
            }
            else
            {
                S = (Polynomial )malloc(sizeof(PolyNode ));
                S->coef = pb->coef;
                S->exp = pb->exp;
                S->next = NULL;
                pc->next = S;
                pc = S;
                pb = pb->next;
            }
    }
    while (pa)
    {
        S = (Polynomial )malloc(sizeof(PolyNode ));
        S->coef = pa->coef;
        S->exp = pa->exp;
        S->next = NULL;
        pc->next = S;
        pc = S;
        pa = pa->next;
    }
    while (pb)
    {
        S = (Polynomial )malloc(sizeof(PolyNode ));
        S->coef = pb->coef;
        S->exp = pb->exp;
        S->next = NULL;
        pc->next = S;
        pc = S;
        pb = pb->next;
    }
    return C;
}
int main()
{
    Polynomial A, B, C, pc;
    char f;
    printf("输入多项式A:\n");
    A = CreateList();
    printf("输入加减符号:\n");
    scanf("%c",&f);
    getchar();       //读掉输入完符号后的回车
    printf("输入多项式B:\n");
    B = CreateList();
    C = (Polynomial )malloc(sizeof(PolyNode ));
    C->next = NULL;
    if(f == '+') C = Add(A , B);
    if(f == '-') C = Minus(A , B);
    pc = C->next;
    while(pc != NULL)
    {
        printf("链表C为:%.2fX^%d " ,pc->coef, pc->exp);
        pc = pc->next;
    }
    printf("\n");
    return 0;
}


 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页