单链表实现多项式相加

首先用两个单链表储存两个多项式,要求输入的时候按指数降序输入。

新建一个单链表res;

接着从两个单链表的头部开始比较;

如果指数不相同,将指数较大者接在res后面,同时res链表与较大者链表的游标向后移动,并且令res游标(此时指向刚刚接上的结点)的next域为空。这个过成相当于从原单链表中直接夺取了一个结点。

如果指数相同,分两种情况。

1.相加后系数为0.直接令两个原多项式的游标向后移动一格,其余什么都不做。

2.相加后系数不为0.将其中任一多项式结点的系数值修改为两结点系数和,然后res链表夺取该结点。

最后如果有一个多项式的结点没有全部接完,直接将该多项式的游标处结点接在res链表后,完成。


代码:

#include <iostream>
#include <stdio.h>
#define HEAD 9999       //如果结点的指数和系数值为head,表示该结点为头结点

using namespace std;

class Node              //结点结构
{
public:
    int exp;
    double coef;
    Node *next;

    Node(int aExp,double aCoef)
    {
        exp = aExp;
        coef = aCoef;
        next = NULL;
    }
};

int main()
{
    int i;
    int n,m;                           //多项式 1 2 的项数
    Node *poly1 = new Node(HEAD,HEAD); //多项式1的头结点
    Node *poly2 = new Node(HEAD,HEAD); //多项式2的头结点

    int exp;                      //指数
    double coef;                  //系数
    Node *index;                  //结点游标

    //输入多项式1
    cin>>n;
    index = poly1;
    for(i=0;i<n;i++)
    {
        cin>>exp>>coef;
        while(index->next != NULL)
            index= index->next;
        index->next = new Node(exp,coef);
    }

    //输入多项式2
    cin>>m;
    index = poly2;
    for(i=0;i<m;i++)
    {
        cin>>exp>>coef;
        while(index->next != NULL)
            index= index->next;
        index->next = new Node(exp,coef);
    }

    Node *res = new Node(HEAD,HEAD);    //结果链表
    index = res;
    Node *p = poly1->next;              //多项式1的游标
    Node *q = poly2->next;              //多项式2的游标

    while(p!=NULL && q!=NULL)
    {
        if(p->exp > q->exp)
        {
            index->next = p;        //将该结点接在结果链表之后
            p=p->next;              //p游标向后移动
            index = index->next;    //结果链表的游标也向前移动(此时指向刚刚接上的结点)
            index->next = NULL;     //将刚接上的结点 next域置为NULL,相当于结果链表将该结点夺了过来
        }
        else if( p->exp < q->exp)
        {
            index->next = q;
            q=q->next;
            index = index->next;
            index->next = NULL;
        }
        else
        {
            if(q->coef+p->coef -0 > -0.000001 && q->coef+p->coef -0 < 0.000001)
            {
                p=p->next;
                q=q->next;
            }
            else
            {
                q->coef += p->coef;
                index->next = q;
                q=q->next;
                index = index->next;
                index->next = NULL;
                p=p->next;
            }

        }

    }

    //如果多项式1或2后仍后结点,直接将其接在结果链表后面
    //多项式1或2有且仅有一个可能后面还有结点
    if(p!=NULL)
        index->next=p;
    if(q!=NULL)
        index->next=q;

    //输出结果链表
    index = res->next;
    while(index != NULL)
    {
        cout<<index->exp<<" "<<index->coef<<endl;
        index = index->next;
    }
    return 0;
}

测试结果:


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值