经典算法:用链表实现多项式加减

设有两个一元多项式:

p(x)= p_0+p_1x+p_2x^2+\cdots+p_nx^n

q(x)= q_0+q_1x+q_2x^2+\cdots+q_mx^m

多项式项的系数为实数,指数为整数,设计实现一元多项式的下列操作:

  • 多项式链表建立:以(系数,指数)(例如(3,0)(-1,1))方式输入项建立多项式,返回所建立的链表的头指针;在输入一个多项式时如果遇到两项指数相同,可以选择舍弃、覆盖或者系数相加操作;
  • 多项式排序:将所建立的多项式按指数非递减(从小到大)进行排序(也可以在建立时保证有序,创建一个有序链表);3,3  2,2  4,4  1,0
  • 多项式相加:实现两个多项式相加操作。操作生成一个新的多项式,原有的两个多项式不变,可以返回生成的多项式的头指针;

P(X)=1+2x+3x^2; Q(x)= 3x+x^3+x^4; P+Q =1+5x+3x^2+x^3+x^4

P(x)-1+2x+3x^2; Q(x)= -2x+x^3; P(x)+Q(x) =1+3x^2+x^3

  • 多项式相减:实现两个多项式相减操作。操作生成一个新的多项式,原有的两个多项式不变,可以返回生成的多项式的头指针;
  • 多项式的输出:按照p_0+p_1x+p_2x^2+\cdots+p_nx^n格式输出多项式;
  • 主函数通过调用多项式链表建立函数,通过文件读取或者控制台输入两个多项式并分别输出;输出排序后的两个多项式;调用多项式相加函数实现多项式相加、相减操作,输出操作结果。

/*    多项式用单链表表示   */
typedef struct Pnode
{
    int deg;
    float coff;
    struct Pnode *next;
} PolyNode;

PolyNode * ListInitiate_poly();

int PolyListDelete(PolyNode *head, int i, PolyNode *x);
int PolyListInsert(PolyNode *head, int i, PolyNode x);
int PolyListLength(PolyNode *head);
int PolyListGet(PolyNode *head, int i, PolyNode *x);
void PolyDestroy(PolyNode *head);


PolyNode*  ListInitiate_poly()//链表初始化
{
    PolyNode * head = (PolyNode *)malloc(sizeof(PolyNode));
    head->next = NULL;
    return head;
}

int PolyListLength(PolyNode *head) //多项式的项数
{
    PolyNode *p = head;
    int size = 0;
    while (p->next != NULL)
    {
        p = p->next;
        size++;
    }
    return size;
}

int PolyListInsert(PolyNode *head, int i, PolyNode x) //在第i个位置上插入一项x
{
    PolyNode *p, *q;  int j;
    p = head;  j = -1;

    while (p->next != NULL && j < i - 1)
    {
        p = p->next;  j++;
    }

    if (j != i - 1)
    {
        printf("插入位置参数错!");  return 0;
    }

    q = (PolyNode *)malloc(sizeof(PolyNode));
    q->coff = x.coff;
    q->deg = x.deg;
    q->next = p->next;  p->next = q;
    return 1;
}
int PolyListDelete(PolyNode *head, int i, PolyNode *x)//从单链表删除1项
{
    PolyNode *p, *s;  int j;
    p = head;  j = -1;
    while (p->next != NULL && p->next->next != NULL && j < i - 1)
    {
        p = p->next;    j++;
    }

    if (j != i - 1)
    {
        printf("删除位置参数错!");      return 0;
    }

    s = p->next;  
    PolyNode tmp;
    tmp.coff = s->coff;
    tmp.deg = s->deg;
    *s = tmp;

    p->next = p->next->next;
    free(s);
    return 1;
}

int PolyListGet(PolyNode *head, int i, PolyNode *x) //取得第i项
{
    PolyNode *p;
    int j;
    p = head;  j = -1;
    while (p->next != NULL && j < i)
    {
        p = p->next;    j++;
    }

    if (j != i)
    {
        printf("取元素位置参数错!");     return 0;
    }

    PolyNode tmp;
    tmp.coff = p->coff;
    tmp.deg = p->deg;

    *x = tmp;

    return 1;
}
void PolyDestroy(PolyNode *head)
{
    PolyNode *p, *p1;
    p = head;
    while (p != NULL)
    {
        p1 = p;  p = p->next;
        free(p1);
    }
    head = NULL;
}

void PolyLinListSort(PolyNode *head) //按照次数排序
{
    PolyNode *curr, *pre, *p, *q;
    p = head->next;   head->next = NULL;
    while (p != NULL)
    {
        curr = head->next;  pre = head;
        while (curr != NULL && curr->deg <= p->deg)
        {
            pre = curr;
            curr = curr->next;
        }
        q = p;   p = p->next;
        q->next = pre->next;  pre->next = q;
    }
}
PolyNode *  PolyADD(PolyNode *head, PolyNode *head2)//二个多项式相加
{
    PolyNode *nhead = ListInitiate_poly();
    PolyNode *p=head->next, *q=head2->next;
    int i = 0, j=0, k=0;
    while (p != NULL && q != NULL)
    {
        if (p->deg < q->deg)
        {
            PolyNode x;
            PolyListGet(head, j++, &x);
            PolyListInsert(nhead, i++, x);
            p = p->next;
        }
        else if (p->deg > q->deg)
        {
            PolyNode x;
            PolyListGet(head2, k++, &x);
            PolyListInsert(nhead, i++, x);
            q = q->next;
        }
        else
        {
            PolyNode x, y, z;
            PolyListGet(head, j, &x);
            PolyListGet(head2,k, &y);
            z.coff = x.coff + y.coff;
            if (z.coff == 0) { j++; k++; q = q->next; p = p->next; continue; }
            z.deg = x.deg;
            PolyListInsert(nhead, i++, z);
            q = q->next; j++;
            p = p->next; k++;
        }
    }
    while (p != NULL)
    {
        PolyNode x;
        PolyListGet(head, j++, &x);
        PolyListInsert(nhead, i++, x);
        p = p->next;
    }
    while (q != NULL)
    {
        PolyNode x;
        PolyListGet(head2, k++, &x);
        PolyListInsert(nhead, i++, x);
        q = q->next;
    }


    return nhead;

}
PolyNode *  PolySUB(PolyNode *head, PolyNode *head2)//二个多项式相减
{

    PolyNode *nhead = ListInitiate_poly();
    PolyNode *p = head->next, *q = head2->next;
    int i = 0, j = 0, k = 0;
    while (p != NULL && q != NULL)
    {
        if (p->deg < q->deg)
        {
            PolyNode x;
            PolyListGet(head, j++, &x);
            PolyListInsert(nhead, i++, x);
            p = p->next;
        }
        else if (p->deg > q->deg)
        {
            PolyNode x;
            PolyListGet(head2, k++, &x);
            x.coff *= -1;
            PolyListInsert(nhead, i++, x);
            q = q->next;
        }
        else  //次数相同的
        {
            PolyNode x, y, z;
            PolyListGet(head, j, &x);
            PolyListGet(head2, k, &y);
            z.coff = x.coff - y.coff;
            if (z.coff == 0) { j++; k++; q = q->next; p = p->next; continue; }
            //系数=0, 就不要添加
            z.deg = x.deg;
            PolyListInsert(nhead, i++, z);
            q = q->next; j++;
            p = p->next; k++;
        }
    }
    while (p != NULL)
    {
        PolyNode x;
        PolyListGet(head, j++, &x);
        PolyListInsert(nhead, i++, x);
        p = p->next;
    }
    while (q != NULL)
    {
        PolyNode x;
        PolyListGet(head2, k++, &x);
        PolyListInsert(nhead, i++, x);
        q = q->next;
    }


    return nhead;

}

//下面是测试用例,用插入法比较简单明了的创建了单链表,完成多项式的表示和计算。

void test_poly()
{
    PolyNode * head1 = ListInitiate_poly();
    PolyNode * head2 = ListInitiate_poly();
    PolyNode x1, x2, x3, x4;
    x1.coff = 1; x1.deg = 4;
    x2.coff = 2; x2.deg = 3;
    x3.coff = 3; x3.deg = 2;
    x4.coff = 4; x4.deg = 1;
    PolyListInsert(head1, 0, x1);
    PolyListInsert(head1, 1, x2);
    PolyListInsert(head1, 2, x3);
    PolyListInsert(head1, 3, x4);

    x1.coff = -1; x1.deg = 4;
    x2.coff = -4; x2.deg = 3;
    x3.coff = 2; x3.deg = 2;
    x4.coff = 3; x4.deg = 1;

    PolyListInsert(head2, 0, x1);
    PolyListInsert(head2, 1, x2);
    PolyListInsert(head2, 2, x3);
    PolyListInsert(head2, 3, x4);

    PolyLinListSort2(head1);
    PolyLinListSort(head2);
    PolyNode* head3 = PolyADD(head1, head2);
    PolyNode* head4 = PolySUB(head1, head2);

}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值