0

I need to implement the assignment operator = on a circular linked list and here is the struct and my code so far. But i am not sure how to copy the coefficient and the power

struct Term {                             // a term on the sparce polynomial

    double coeff;                         // the coefficient of each term

    int power;                                    // the degree of each term

    Term *prev;                     // a pointer to the previous higher term
    Term *next;                          // a pointer to the next lower term
};
int size;                               // # terms in the sparce polynomial
Term *head;               // a pointer to the doubly-linked circular list of

Polynomial& Polynomial::operator=(const Polynomial &rightpoly){

 // check for self assignment
 if(&rightpoly !=this){
     // clear this
     if(head !=NULL)
         delete this;
     if(rightpoly.head == NULL)
         return *this;
     // make new node for the head
     Term *ptr = new Term;

     head = ptr;
     Term *curRight = rightpoly.head ->next;
     Term *curLeft = ptr;

     // traverse the list, making a new node and copying data
     // until rightpoly is empty
     while (curRight !=NULL) {
         curLeft ->next = new Term;
         curLeft = curLeft ->next;
         curRight = curRight ->next;
     }
     curLeft ->next = NULL;
 }
 return *this;
}
  • 1
    Before doing this, do you have a working copy constructor (that does not call the assignment operator) and destructor? If so, the assignment operator is simple. If not, write those two functions first, the assignment operator leave for last (since it will be simple). – PaulMcKenzie Apr 05 '17 at 22:38
  • @PaulMcKenzie I do have a copy constructor and destructor for this class. –  Apr 05 '17 at 22:45
  • Extending on Paul McKenzie's comment, make use of other stuff you have written. For example, if you already have an insert function for adding coefficients, call it to add copies of the source polynomial's coefficients. Why write the code twice? Here is an explanation of the simplification Paul McKenzie's talking about: [What is the copy-and-swap idiom?](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – user4581301 Apr 05 '17 at 22:52
  • Yes, the assignment operator would only consist of creating a temp object, swapping 4 things from the temp to `*this`, and then returning `*this;`. Much easier, safer, and guaranteed to work (providing you have a correct copy constructor and destructor) than writing frightening pointer code twice (once in the copy ctor and again for the assignment op). – PaulMcKenzie Apr 05 '17 at 23:03
  • @Dabera For more reference and a good example, see [this question and answer](http://stackoverflow.com/questions/42451647/how-to-implement-an-assignment-operator-in-linked-list-class) – PaulMcKenzie Apr 05 '17 at 23:10
  • `delete this;` is not appropriate here because you want the object to remain valid after the function returns. `delete this` is also completely undefined behavior is the object is an automatic variable (allocated on the stack). Any access to members after `delete this;` is also invalid. – Christopher Oicles Apr 06 '17 at 00:33

0 Answers0