Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:00:35+00:00 2026-06-14T23:00:35+00:00

I have a linked list that is represented as struct term{ double coef; unsigned

  • 0

I have a linked list that is represented as

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

then i have a polynomial class

class Polynomial{
public:
    Polynomial & operator+ (const Polynomial & ) const;
private:
    term *ptr

and i am trying to do an addition overloaded operator, but what i tried just give me some random part of the polynomial that is in the middle.

Polynomial & Polynomial::operator+(const Polynomial & poly) const{
    Polynomial p2 = *this;
    term * temp = (*this).ptr;
    while(temp->next != NULL){
        temp = temp->next;
    }
    temp->next = poly.ptr;
    return p2;
}

and also when i have 2 polynomials, one is a copy of another just then added one more term, then when i try to use the addition operator, the first polynomial is bigger, like the second polynomial is added to it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:00:37+00:00Added an answer on June 14, 2026 at 11:00 pm

    You’re operator+() is seriously whacked. Consider the idea of “term” ownership. Each polynomial has a linked list of terms. It owns this list (at least it better). Now consider this brief analysis of your operator +():

    Polynomial Polynomial::operator+(const Polynomial & poly) const 
    {
        // hopefully creates a deep copy of *this
        Polynomial p2 = *this;
    
        // walk *our* terms (not the copy in p2??) to find the end.
        term * temp = (*this).ptr;
        while(temp->next != NULL)
            temp = temp->next;
    
        // once we find the end, we then *LINK* the params term-list
        //  which *they* are **supposed** to own, to our list. (and 
        //  p2 is still out there with a copy of our original content).
        temp->next = poly.ptr;
    
        // now we return p2, still holding a copy of our former self,
        // and we now have a cross-linked term list between us and the 
        // parameter poly
        return p2;
    }
    

    I sincerely hope it is evident what is wrong with that. For this to work correctly, your operator should be returning a by-val, (which it is, hooray!), and manufacture that thing correctly:

    • Make a copy (lets call it p2) of *this (you have that)
    • Find the end of the term list owned by p2
    • Duplicate all terms in the rhs parameter of operator +(const Polynomial* rhs), linking the copies one-by-one to the tail of p2‘s term list. Note: the tail will move with each new term linked.
    • Return p2 by val. If your copy-ctors and destructors are doing their job, everything should come out fine. When done, both *this, and rhs should be untouched.

    Thats about the extent I can offer. Good luck.

    PS: For extra-credit-bonus-round, sort your terms in your list as you insert them. This will get you one step closer to a like-degree merge, which will be the backbone of operator +=() and greatly assist your operator +(). The latter literally degenerates to Polynomial p2 = *this; p2 += poly; return p2;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a linked list class that stores a linked collection of entities. I've
So basically I have a Linked List class that has all of the constructor
I have a linear linked list that consists of nodes: class Node{ Object data;
I have a generic linked-list that holds data of type void* I am trying
I have a Linked List and I want to implement a function: Random_Shuffle_List (struct
I have a singly-linked list containing 1 value in each element. struct listElement {
I have the following function defined inside my linked list class. The declaration in
I have a linked list that I want to sort part of, eg: std::sort(someIterator,
Say I have a linked list called L that stores linked lists so: LinkedList
I have an add method for creating a linked list that is unordered and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.