0

Im a little confused on how to implement an copy assignment on a doubly linked List. I managed to get the copy constructor working but im sure on the assignment. Im trying to do this without the copy and swap method.

List.H

class List 
{
public:
    List();
    ~List();
    List(const List& c);
    List& operator= (const List& t);
private:
    List *Next;
    List *Prev;
    Node *Head;

List.cpp

List::~List()
{
    Node* move = Head;
    while (move!=NULL)
    {
        Node *temp = move->Next;
        delete move;
        move = temp;
    }
}

List::List(const List& c)
{
    name = c.name;
    Prev = c.Prev;
    Next = c.Next;
    Node* dummy, * current;
    Head= dummy = new Node();
    current = c.Head;
    while (current)
    {
        dummy->Next = new Node(*current);
        current = current->Next;
        dummy = dummy->Next;
    }

    Node* temp = Head;
    Head = Head->Next;
    delete temp;
}

List& List::operator=(const List& t)
{
    Next = t.Next;
    return *this;
}

Would I also have to traverse each node in the assignment operator as well?

Edit So this is what I have now. The problem is when im getting the data from the list it is null.

List& List::operator=(const List& that)
{

    if (this != &that)
    {
        while (Head)
        {
            Node* temp = Head;
                Head = Head->Next;
                delete temp;
        }
        Node* dummy, * current;
        Head = dummy = new Node();
        current = that.Head;
        while (current)
        {
            dummy->Next = new Node(*current);
            current = current->Next;
            dummy = dummy->Next;
        }
    dummy->Next = nullptr;
}
return *this;
}

4
  • 1
    What's wrong with copy and swap? It has the added benefit of being exception-safe (assuming your copy is exception-safe). Commented Jan 18, 2020 at 19:03
  • copy-ctor and operator= both are doing shallow copy. Which will can lead to undefined behavior due to deleteing same memory from two different objects Commented Jan 18, 2020 at 19:13
  • @TruthSeeker The copy-ctor makes a deep copy of the list. Only Prev and Next whatever they are, are not deep-copied, but they are also not deleted in the destructor, so it should be fine. Commented Jan 18, 2020 at 19:21
  • @churill: yes, you are right even though it is assigned it not being deleted. But not sure the significance of Next and Previous. @Jay any explanation for Next and Previous usecase? Commented Jan 18, 2020 at 19:29

1 Answer 1

1

Short answer is YES.

  1. copy-constructor
    List L1;
    List L2(L1);
  1. operator=
    List L1;
    List L2;
    L2 = L1;

In both the cases, L1 has to be copied to L2 and L1 should be unchanged after copy or assignment. Hence content of every node has to be replicated to newly created node.

Copy Constructor looks something like this:

List::List(const List& c)
{
    Node start;
    Node* dummy = &start;
    Node* CurrentNode = c.Head;
    while (CurrentNode)
    {
        dummy->next  = new Node(*CurrentNode);//New node created with content of *CurrentNode
        dummy = dummy->Next;
        CurrentNode  = CurrentNode->Next;
    }

    dummy->next = nullptr;
    Head = start.next;
}

And assignment operator like this:

List& List::operator=(const List& that)
{
    if (this != &that) //avoid self assignment like List L1;L1=L1;
    {
       while (Head)//Delete exist nodes
       {
           Node* temp = Head;
           Head = Head->Next
           delete temp;
       }

        Node start;
        Node* dummy = &start;
        Node* thatHead = that.Head;
        while (thatHead)
        {
            dummy->next  = new Node(*thatHead);//New node created with content of *thatHead 
            dummy = dummy->Next;
            thatHead = thatHead->Next;
        }
        dummy->next = nullptr;
    }
    return *this;
}
Sign up to request clarification or add additional context in comments.

2 Comments

When doing the while(that) i get an exprssion must have bool type error and when creating a new node with the content of *that i get "no operator matches these operands. wouldnt the While loop need to be while (&that).
@Jay: I have updated to snippet. Here that is List and we need to traverse the List Node

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.