linked list copy constructor

This is a discussion on linked list copy constructor within the C++ Programming forums, part of the General Programming Boards category; I am trying to write the copy constructor for a linked list class and I am having problems. The code ...

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    linked list copy constructor

    I am trying to write the copy constructor for a linked list class and I am having problems. The code compiles (which doesn't mean anything), but when I try to implement the constructor it doesn't copy the data within the nodes and I always get a memory error.

    Here is the code for the copy constructor:

    Code:
    linkList::linkList(const linkList &originalList)
    {
    	node * tmpPtr = new node;
    	tmpPtr = originalList._firstElement;
    	linkList tmpList(tmpPtr);
    
    	while (tmpPtr != NULL)
    	{
    		node * newNode = new node;
    		newNode = tmpPtr;
    		tmpPtr = tmpPtr->next;
    		_numElements ++;
    	}
    
    }
    node is the node class and linkList is the list class. _firstElement and _numElements are members of the linkList class. next is a member of the node class.
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    Code:
    linkList::linkList(const linkList &originalList)
    {
    	node * tmpPtr = new node;
    	tmpPtr = originalList._firstElement;
    	linkList tmpList(tmpPtr);
    
    	while (tmpPtr != NULL)
    	{
    		node * newNode = new node;
    
    		newNode->data = tmpPtr->data;
    
    		_lastElement->next = newNode;
    		_lastElement = newNode;
    		_numElements ++;
    
    		tmpPtr = tmpPtr->next;
    
    
    		if (tmpPtr->next == NULL)
    		{
    			break;
    		}
    	}
    
    }
    Instead of dereferencing the node data, I am copying the data member. But I still can't get this code to run. I am still having memory problems.
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you should be implementing the copy constructor using your lists iterators to iterate over the list to be copied and using one of your insert functions to do the copying.

    so you basically want something like...
    Code:
    ...copy con
    {
       for( MyListIter iter = MyList.begin; iter != MyList.end(); ++iter)
        InsertAtTail( iter->Data);
    }
    That assumes your list is half decently designed.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    _lastElement->next = newNode;
    _lastElement = newNode;

    The above lines would appear to append the new node at the end of the list, assuming _lastElement is assigned as starting at head of new list somewhere and is advanced by one step each time through the loop like tmpPtr is. If you already have a method to insert new node at end of list, then you can just call it in this function without the need to rewrite an append at the end function here.

    I think the following sequence means that the last node in the original list won't get copied. Is that what you want? Some lists don't include data in lastElement so maybe so. Many times, lastElement does have data in it, in which case this is a bug. Can't say for sure based on code posted though.

    tmpPtr = originalList._firstElement;
    while (tmpPtr != NULL)
    {
    tmpPtr = tmpPtr->next;

    if (tmpPtr->next == NULL)
    {
    break;
    }
    }

    Also why bother to assign the address of a new node to tmpPtr if you aren't going to use it? Drop the first line below.

    node * tmpPtr = new node;
    tmpPtr = originalList._firstElement;


    Likewise the following line appears unnecessary, for the code posted at least.

    linkList tmpList(tmpPtr);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21