数据结构之多项式add

多项式加法链表实现
本文介绍了一种使用链表来实现多项式加法的方法。通过定义链表节点结构和相关操作函数,实现了多项式的输入、相加及输出功能。文章提供了完整的C++代码示例。

测试数据不多,可能有bug,不过至少现在还能用~~

代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;
int n,m;
typedef struct node
{
    int coef,exp;
    struct node *next;
}*Linklist,Lnode;

Linklist end_create(int num)
{
    Linklist temp,head=(Linklist)malloc(sizeof(Lnode));
    head->next=NULL,temp=head;
    int a,b,i;
    for(i=1;i<=num;i++)
    {
        scanf("%d%d",&a,&b);
        Linklist now=(Linklist)malloc(sizeof(Lnode));
        now->coef=a,now->exp=b;
        temp->next=now,temp=temp->next;
    }
    temp->next=NULL;
    return head;
}

void Destroy_list(Linklist head)
{
    Linklist node=head->next,temp=head;
    while(node&&temp)
    {
        temp=node,node=node->next;
        free(temp);
    }
    free(head);

}

void add(Linklist polya,Linklist polyb)
{
    Linklist p=polya->next,q=polyb->next,tail=polya;
    Linklist temp;
    ///利用了本来已有的链进行计算(以polya为头结点)
    while(p&&q)
    {
        if(p->exp<q->exp)
        {
            tail->next=p,tail=p,p=p->next;///tail为当前已经在最终表达式里面的最后一项
            ///同时也是p或者q的前驱
        }
        else if(p->exp>q->exp)
        {
            tail->next=q,tail=q,q=q->next;
        }
        else
        {
            if(p->coef+q->coef!=0)
            {
                p->coef=p->coef+q->coef;
                tail->next=p,tail=p,p=p->next;
                temp=q,q=q->next,free(temp);///注意free掉没用的q结点
            }
            else
            {
                temp=p,p=p->next,free(temp);
     ///什么?和tail没有关系?是的,因为tail本身的结点不会丢失,还是指向同一个结点
                temp=q,q=q->next,free(temp);
            }
        }
        if(p!=NULL)
        {
            tail->next=p;///直接连上p的剩余部分(也是完整的链)
        }
        else
        {
            tail->next=q;
        }
    }
}

void print(Linklist polya)
{
    Linklist temp=polya->next;
    while(temp)
    {
        printf("%d %d\n",temp->coef,temp->exp);
        temp=temp->next;
    }
}
int main()
{
    scanf("%d%d",&n,&m);///两个多项式分别有n项和m项
    Linklist polya=end_create(n);
    Linklist polyb=end_create(m);
    ///两个多项式的指数单增
    add(polya,polyb);
    //printf("%d %d\n",polya->next->coef,polya->next->exp);
    //printf("%d %d\n",polyb->next->coef,polyb->next->exp);
    print(polya);
    Destroy_list(polya),Destroy_list(polyb);
    return 0;
}

多项式相加是指将两个或多个多项式相加得到一个新的多项式。在数据结构中,可以使用链表或数组来表示多项式。 一种常见的数据结构是使用链表表示多项式。每个节点包含两个成员:系数和指数。链表的每个节点代表一个单项式,多个节点组成一个多项式。通过遍历链表,可以将相同指数的节点合并,并将系数相加得到新的系数。 另一种数据结构是使用数组表示多项式。数组的索引表示指数,数组的值表示系数。通过遍历数组,可以将相同指数的系数相加得到新的系数。 以下是一种基于链表数据结构设计多项式相加的示例代码: ```python class Node: def __init__(self, coefficient, exponent): self.coefficient = coefficient self.exponent = exponent self.next = None def add_polynomials(poly1, poly2): result = None current = None while poly1 and poly2: if poly1.exponent > poly2.exponent: coefficient = poly1.coefficient exponent = poly1.exponent poly1 = poly1.next elif poly1.exponent < poly2.exponent: coefficient = poly2.coefficient exponent = poly2.exponent poly2 = poly2.next else: coefficient = poly1.coefficient + poly2.coefficient exponent = poly1.exponent poly1 = poly1.next poly2 = poly2.next if coefficient != 0: node = Node(coefficient, exponent) if result is None: result = node current = node else: current.next = node current = node while poly1: node = Node(poly1.coefficient, poly1.exponent) current.next = node current = node poly1 = poly1.next while poly2: node = Node(poly2.coefficient, poly2.exponent) current.next = node current = node poly2 = poly2.next return result ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值