当前位置:网站首页>6-43 sum of ordered sparse polynomials

6-43 sum of ordered sparse polynomials

2022-06-22 09:35:00 Xia ~ Chen

This problem is to find the sum of two ordered polynomials , The input polynomial has two terms : Coefficients and exponents .

Polynomials are stored in chains , And arrange them according to the index from small to large .

The output is also in the order of exponent from small to large .

( Input data in descending order of index , Input 0 0 The hour means the end . The construction part has been completed , No need to write it yourself )

To simplify the construction of linked list , The default construction linked list is the supervised single linked list .

Function interface definition :

ptr add(ptr ha,ptr hb);

add Are two ordered linked lists ha,hb Do the addition , Form a new linked list and return , The original list ha,hb Retain .

Sample referee test procedure :

#include <stdio.h>
#include <math.h>
#include <malloc.h>
#define N 5

typedef struct node
{
    float ceof;
    int exp;
    struct node *next;
}node,*ptr;

ptr creat();// The tail of the table is inserted to construct the linked list , It's not here 
ptr add(ptr ha,ptr hb);

void output(ptr h)
{
    ptr p;
    p=h->next;
    while(p!=NULL)
    {
        printf("%+.1fx^%d",p->ceof,p->exp);
        p=p->next;
    }
    printf("\n");
}

int main()
{
    ptr h1,h2,h3;
    h1=creat();
    h2=creat();
    h3=add(h1,h2);
    output(h1);
    output(h2);
    output(h3);
    return 0;
}

/*  Please fill in the answer here  */

sample input :

-19  0
21  2
90.2  5
100  10
18  92
0  0
19  0
-21  2
65  4
-8  5
43.6  14
0  0

No blank lines at the end

sample output :

-19.0x^0+21.0x^2+90.2x^5+100.0x^10+18.0x^92
+19.0x^0-21.0x^2+65.0x^4-8.0x^5+43.6x^14
+65.0x^4+82.2x^5+100.0x^10+43.6x^14+18.0x^92

  Ideas : According to the title, we need to add and subtract the two ordered linked lists , The construction of the linked list has been completed for us ( The damn question hides this part of the code , You can contact me if you need me ), First, keep the original linked list , We definitely need to create a new linked list to store the results of adding coefficient polynomials , When you see a friend here, you may ask , How to ensure order ? Um. .... The title has been made for you , Never mind , Just compare and add directly , I don't say much nonsense , Code up .

Code up : 

ptr add(ptr ha, ptr hb)
{
    ptr h, s, p;
    h = s=(ptr)malloc(sizeof(node));// Create a new linked list to store the results of the addition of the two linked lists 
    ha=ha->next;// Leading one-way linked list , Remember that the assignment of the tail node is null 
    hb=hb->next;
    while (ha&&hb)// This is to calculate the same length of two linked lists , Some watches may be long , Some may be short , This time in short                 
                  // Compare based on the length of the linked list , The long part , Just bring it here 
    {
        if (ha->exp < hb->exp) // Compare index sizes 
        {
            p = (ptr)malloc(sizeof(node));
            p=ha;
            ha = ha->next;
            s->next = p;
            s = p;
        }
        else if(ha->exp==hb->exp)
        {
            p = (ptr)malloc(sizeof(node));
            if(ha->ceof+hb->ceof==0)
            {
            ha=ha->next;
            hb=hb->next;
            }
            else
            {
                p->exp=ha->exp;
                p->ceof=ha->ceof+hb->ceof;
                ha=ha->next;
                hb=hb->next;
                s->next=p;
                s=p;
            }
        }
        else
        {
            p = (ptr)malloc(sizeof(node));
            p=hb;
            hb = hb->next;
            s->next = p;
            s = p;
        }
    }
    while (ha != NULL)// Operate on the long part 
    {
        p = (ptr)malloc(sizeof(node));
        p=ha;
        ha = ha->next;
        s->next = p;
        s = p;
    }
    while (hb != NULL)
    {
        p = (ptr)malloc(sizeof(node));
        p=hb;
        hb = hb->next;
        s->next = p;
        s = p;
    }
    s->next =NULL;// The value of the tail node is set to null 
    return h;// Return the head node of the new linked list , I still have to output 
}

 

原网站

版权声明
本文为[Xia ~ Chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220522574326.html