user1335175
user1335175

Reputation: 189

Doubly linked list copy constructor and program crash

I am writing my first doubly linked list program, and there is a problem with my copy constructor. I am trying to use an insert function in my copy constructor, which takes position and value and inserts the value into a list, but upon calling the copy constructor, my program crashes, with no error messages.

Here is my copy constructor and my insert function.

Sequence::Sequence(const Sequence& other)
{
    m_head=NULL;
    m_numberOfItems=other.m_numberOfItems;
    Node* tempNode;
    int i;
    for(i=0,tempNode=other.m_head;tempNode !=NULL ;tempNode=tempNode->m_next,i++)
    {

        insert(i,tempNode->m_data);
    }
}


bool Sequence::insert(int pos, const ItemType& value)
{

    if(pos<0 || pos > size())
        return false;
    else
    {
        Node* tempNode;
        Node* newNode = new Node;


        if(pos==size()) //This will happend if inserting 1st element or inserting at the end.
        {



            if(empty()==true) //in Case if there is 0 elements in the Sequence.
            {
                newNode->m_data=value;
                newNode->m_previous=NULL;
                newNode->m_next=NULL;
                m_head=newNode;
                m_numberOfItems++;
            }
            else  //Inserting element at the end.
            {
                for(tempNode=m_head;tempNode->m_next!=NULL; tempNode=tempNode->m_next)
                ;

                newNode->m_previous=tempNode;
                newNode->m_next=NULL;
                newNode->m_data=value;
                tempNode->m_next=newNode;
                m_numberOfItems++;
            }
        }


        else //inserting at position 0 or somewhere in the middle.
        {
            tempNode=m_head;
            newNode->m_data=value;

            if(pos==0) //If we are inserting in 0 position of non empty sequence.
            {
                newNode->m_previous=NULL;
                tempNode->m_previous=newNode;
                newNode->m_next=tempNode;
                m_head=newNode;

            }
            else
            {
                int tempPosition=0;

                while(tempPosition<pos)
                {
                    tempPosition++;
                    tempNode=tempNode->m_next;
                }

                newNode->m_next=tempNode;
                newNode->m_previous=tempNode->m_previous;
                tempNode->m_previous=newNode;
                newNode->m_previous->m_next=newNode;
            }
            m_numberOfItems++;
        }
        return true;
    }
}

Can someone please explain what is wrong here, and what will be the best way to write a copy constructor for Doubly linked lists?

Thank you.

Upvotes: 1

Views: 233

Answers (1)

Christophe
Christophe

Reputation: 73366

Well, in your copy constructor you set the size before anything else:

m_numberOfItems=other.m_numberOfItems;

Consequences:

When you insert(), you need the correct size to make the correct decisions. So in the folowing statement :

    if(pos==size()) //This will happend if inserting 1st element or inserting at the end.

you will jump immediately to the else block:

        ...
   else //inserting at position 0 or somewhere in the middle.
    {
        tempNode=m_head;
        ...

But m_head is still null when you insert the first item. So when you reach the following statement, it will hurt :

           tempNode->m_previous=newNode;   /// <<<<<=== OUCH !!! 

Solution:

Initialize m_numberOfItems to 0 and let insert() increment it until it reaches the right value on its own.

Upvotes: 2

Related Questions