This is my derivative function. This code works correctly but does not delete the constant term. It sets directly to 0 and writes to the result. how can i fix this? Thank you..
EX:
Input: "-x^3-6x^2+ 4x+22"
Output: “-3.0x^2 -12x +4.0 + 0.0”
How can i delete this 0.0?

struct PolyNode { 
    double coef;              
    int exp;                  
    struct PolyNode* next;    
}; 
/////////////////////////////////////// 
PolyNode* Derivative(PolyNode* poly) { 
    PolyNode* current = new PolyNode; 
    current = poly; 

    //Just goes through entire list 
    while (current != NULL) { 
        if (current->exp == 0) { 
            current->coef = 0; 
            current->exp = 0; 
            current = current->next; 
        } 
        else { 
            current->coef = current->coef * current->exp; 
            current->exp = current->exp - 1; 
            current = current->next; 
        } 
    } 


    return poly;

Dani AI

Generated

Short diagnosis: the extra "0.0" comes from leaving the original constant node (term with exponent 0) in the list and only zeroing its coef instead of removing that node. There is also a pointer bug in the posted snippet (allocating a node then immediately overwriting the pointer) which leaks memory. Two safe approaches follow: build a fresh derivative list (simpler and less error-prone), or update the list in place and unlink/delete original constant nodes.

Build a new list (recommended):

// assumes PolyNode is already defined
PolyNode* DerivativeCopy(const PolyNode* poly) {
    PolyNode* head = nullptr;
    PolyNode* tail = nullptr;
    for (const PolyNode* p = poly; p != nullptr; p = p->next) {
        if (p->exp == 0) continue;             // skip original constants
        double nc = p->coef * p->exp;
        int ne = p->exp - 1;
        PolyNode* n = new PolyNode();
        n->coef = nc; n->exp = ne; n->next = nullptr;
        if (!head) head = tail = n;
        else { tail->next = n; tail = n; }
    }
    return head;
}

In-place update + remove zero-derivative nodes:

PolyNode* DerivativeInPlace(PolyNode* poly) {
    PolyNode* prev = nullptr;
    PolyNode* cur = poly;
    while (cur) {
        if (cur->exp == 0) {             // original constant: remove node
            PolyNode* rem = cur;
            cur = cur->next;
            if (prev) prev->next = cur; else poly = cur;
            delete rem;
            continue;
        }
        cur->coef *= cur->exp;           // compute derivative
        cur->exp -= 1;
        prev = cur;
        cur = cur->next;
    }
    return poly;
}

Extra notes: when building a new list, free the original list if it is no longer needed to avoid leaks. Both approaches preserve constants produced by differentiating degree-1 terms (e.g., 4x -> 4.0) but remove constants that were present in the original polynomial (their derivative is zero). Also fix the pointer allocation bug in the original code (do not new a node and immediately overwrite that pointer). As is learning data structures, the copy approach is easier to reason about; and as noted, avoid cross-post duplication.

Recommended Answers

All 3 Replies

I see you crossposted but agree the question is missing input and corresponding output statements.

Thanks for your comment. So what do I have to do to fix these mistakes? I'm pretty new at data structures

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.