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.