简体   繁体   中英

derivative of the polynomial in a linked list

For some reason when i am trying to do my derivative it just does a derivative of the one item not the whole polynomial.

struct term{
    double coef;
    unsigned deg;
    struct term * next;
    };

I have a struct and then also a class Polynomial with deep copy constructor and = constructor. In a private class i have a term* ptr

here is my code for the derivative

void Polynomial::derivative (Polynomial *p){
    term *x;
    if ( ptr == NULL)
        return ;
    term *temp;
    temp = ptr;
    while (temp != NULL){
       if ( ptr == NULL){
            ptr = new term ;
            x = ptr ;
        }
        else{
            x -> next = new term ;
            x = x -> next ;
        }
        x-> coef = temp -> coef * temp -> deg;
        x-> deg = temp -> deg - 1;
        temp = temp -> next;

    }
    ptr=x;
}

so when i try to derivative of 3x^4 + 3x^4 + 6x^7 + 3x^4 + 3x^4 + 6x^7 + 2x^9 i get 18x^8

I was looking over the code and have no idea why does it do that for just the last term, since it is a while loop and should go from beginning till NULL and do the derivative.

You're getting the last term because of these two lines:

in your else condition:

x = x -> next

and your final assignment:

ptr = x;

Consequently, this also leaks memory, since all those pretty terms you allocated prior are now in the ether. You were leaking the old ones anyway, so this really needs a rethink regardless.

I strongly recommend that since your Polynomial class supports full copy construction and assignment operation, you create a new derivative polynomial from this one, and return that . if the caller wants this one transformed they can poly = poly.derivative(); themselves.

Example of derivative generator (as opposed to transformer). And as a bonus, eliminates all constant terms when generating the derivative.

Polynomial Polynomial::derivative() const
{
    Polynomial poly;

    const term *p = ptr;
    while (p)
    {
        if (p->deg != 0) // skip constant terms 
        {
            // add term to poly here (whatever your method is called)
            poly.addTerm(p->coef * p->deg, p->deg-1);
        }
        p = p->next;
    }
    return poly;
}

This allows this kind of generation: (note p1 is unchanged by derivative() ):

Polynomial p1;
... populate p1...
Polynomial p2prime = p1.derivative();

And for something really enjoyable:

Polynomial p1;
... populate p1...
Polynomial p2prime2 = p1.derivative().derivative();

Anyway, I hope that makes sense.

PolyNodeN* makeDerivate(PolyNodeN* poly)
{
    PolyNodeN* head = new PolyNodeN();
    PolyNodeN* tmp = new PolyNodeN();
    int a = poly->exp;

    int * results = new int[a];
    int * exponents = new int[a];

    for (int i = 0; i < a; i++)
    {
        results[i] = exponents[i] = 0;
    }
    for (poly; poly != nullptr; poly = poly->next)
    {
        results[poly->exp - 1] = poly->koef*poly->exp;
        exponents[poly->exp - 1] = poly->exp - 1;
    }
    head = new PolyNodeN(exponents[a - 1], results[a - 1]);
    tmp = head;

    for (int i = a - 2; i >= 0; i--)
    {
        tmp->next= new PolyNodeN(exponents[i], results[i],tmp);
        tmp = tmp->next;
    }
    tmp->next = nullptr;
    return head;
}

which derivate ? simple ...

PolyNodeN* makeDerivate2(PolyNodeN* poly,int ext)
{
    PolyNodeN* temp = new PolyNodeN();
    temp = temp->makeDerivate(poly);
    for (int i = ext-1; i > 0; i--)
        temp = temp->makeDerivate2(temp, i);
    return temp;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM