链表(篇5)用链表实现多项式相加

使用链接实现两个多项式相加

例:

输入:
     第一数= 5x ^ 2 + 4x ^ 1 + 2x ^ 0
     第二数= 5x ^ 1 + 5x ^ 0
输出:
        5x ^ 2 + 9x ^ 1 + 7x ^ 0
输入:
     第一数= 5x ^ 3 + 4x ^ 2 + 2x ^ 0
     第二数= 5x ^ 1 + 5x ^ 0
输出:
        5x ^ 3 + 4x ^ 2 + 5x ^ 1 + 7x ^ 0

这里写图片描述


代码


// C++ program for addition of two polynomials
// using Linked Lists
#include<bits/stdc++.h>
using namespace std;

// Node structure containing power and coefficient of variable
struct node
{
    int coeff;
    int pow;
    struct node *next;
};

// Function to create new node
void create_node(int x, int y, struct node **temp)
{
    struct node *r, *z;
    z = *temp;
    if(z == NULL)
    {
        r =(struct node*)malloc(sizeof(struct node));
        r->coeff = x;
        r->pow = y;
        *temp = r;
        r->next = (struct node*)malloc(sizeof(struct node));
        r = r->next;
        r->next = NULL;
    }
    else
    {
        r->coeff = x;
        r->pow = y;
        r->next = (struct node*)malloc(sizeof(struct node));
        r = r->next;
        r->next = NULL;
    }
}

// Function Adding two polynomial numbers
void polyadd(struct node *poly1, struct node *poly2, struct node *poly)
{
while(poly1->next && poly2->next)
    {
        // If power of 1st polynomial is greater then 2nd, then store 1st as it is
        // and move its pointer
        if(poly1->pow > poly2->pow)
        {
            poly->pow = poly1->pow;
            poly->coeff = poly1->coeff;
            poly1 = poly1->next;
        }

        // If power of 2nd polynomial is greater then 1st, then store 2nd as it is
        // and move its pointer
        else if(poly1->pow < poly2->pow)
        {
            poly->pow = poly2->pow;
            poly->coeff = poly2->coeff;
            poly2 = poly2->next;
        }

        // If power of both polynomial numbers is same then add their coefficients
        else
        {
            poly->pow = poly1->pow;
            poly->coeff = poly1->coeff+poly2->coeff;
            poly1 = poly1->next;
            poly2 = poly2->next;
        }

        // Dynamically create new node
        poly->next = (struct node *)malloc(sizeof(struct node));
        poly = poly->next;
        poly->next = NULL;
    }
while(poly1->next || poly2->next)
    {
        if(poly1->next)
        {
            poly->pow = poly1->pow;
            poly->coeff = poly1->coeff;
            poly1 = poly1->next;
        }
        if(poly2->next)
        {
            poly->pow = poly2->pow;
            poly->coeff = poly2->coeff;
            poly2 = poly2->next;
        }
        poly->next = (struct node *)malloc(sizeof(struct node));
        poly = poly->next;
        poly->next = NULL;
    }
}

// Display Linked list
void show(struct node *node)
{
while(node->next != NULL)
    {
    printf("%dx^%d", node->coeff, node->pow);
    node = node->next;
    if(node->next != NULL)
        printf(" + ");
    }
}

// Driver  program
int main()
{
    struct node *poly1 = NULL, *poly2 = NULL, *poly = NULL;

    // Create first list of 5x^2 + 4x^1 + 2x^0
    create_node(5,2,&poly1);
    create_node(4,1,&poly1);
    create_node(2,0,&poly1);

    // Create second list of 5x^1 + 5x^0
    create_node(5,1,&poly2);
    create_node(5,0,&poly2);

    printf("1st Number: "); 
    show(poly1);

    printf("\n2nd Number: ");
    show(poly2);

    poly = (struct node *)malloc(sizeof(struct node));

    // Function add two polynomial numbers
    polyadd(poly1, poly2, poly);

    // Display resultant List
    printf("\nAdded polynomial: ");
    show(poly);

return 0;
}

output:

1st Number: 5x^2 + 4x^1 + 2x^0
2nd Number: 5x^1 + 5x^0
Added polynomial: 5x^2 + 9x^1 + 7x^0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值