Creating a copy constructor for a linked list?

You have to be careful with Step 1 and part of Step 2. Step 1 should allocate a new node and use that as a head. In Step 2, the part about next = v.

Next unless your intention is to make a shallow copy, which it most likely is not.

You have to be careful with Step 1 and part of Step 2. Step 1 should allocate a new node and use that as a head. In Step 2, the part about next = v.

Next, unless your intention is to make a shallow copy, which it most likely is not. When you copy a container such as a linked list, you probably want a deep copy, so new nodes need to be created and only the data copied over. Here is a picture depicting the differences: For more information about the difference between shallow and deep copies, see the Wikipedia article on object copying.

2 Ooh! Pictures! That will definitely help clarify.

+1 – Mooing Duck Oct 18 at 18:58.

You shouldn't set this->head = v.head. Because the head is simply a pointer. What you need to do is to create a new head and copy the values individually from v.

Head into your new head. Otherwise you'd have two pointers pointing to the same thing. You then would have to create a temporary Elem pointer that starts with v.

Head and iterate through the list, copying its values to new Elem pointers into the new copy. See above.

2 Note: When you copy it's values, do not copy it's pointer. In general, don't ever copy a pointer in a copy constructor. Copy the object being pointed at.

– Mooing Duck Oct 18 at 18:39.

It should copy pri - easy. It should copy info- easy as well. And if next is not null, it should copy it, too.

How can you copy next? Think recursive: Well, next is an Elem *, and Elem has a copy constructor: Just use it to copy the referenced Elem and refer to it. You can solve this iteratively, too, but the recursive solution is much more intuitive.

True, this is quite easy if Elem has a copy constructor – Mooing Duck Oct 18 at 18:40 We are intentionally not doing recursion yet, so we can see what happens more easily. He showed us the recursive way and deliberately said we'd fail if we did it this way. O_O – Joshua Oct 20 at 2:25 @Joshua: You should mention such constraints.

– Landei Oct 20 at 12:08.

So here is my answer (don't know if that fits to your homework or not - instructors tend to have their own ideas sometimes ;): Generally a copy constructor should "copy" your object. I.e. Say you have linkedList l1, and do a linkedList l2 = l1 (which calls linkedList::linkedList(l1)), then l1 and l2 are totally separate objects in the sense that modification of l1 doesn't affect l2 and vice versa.

When you just assign pointers you won't get a real copy, as dereferencing and modifying either of them would affect both objects. You rather want to make a real deep copy of every element in your source list (or do a copy-on-demand only, if you want to be fancy).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions