Jay
Jay

Reputation: 41

How to implement a copy assignment on a doubly linked List?

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;
}

Upvotes: 0

Views: 1396

Answers (1)

TruthSeeker
TruthSeeker

Reputation: 1579

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;
}

Upvotes: 1

Related Questions