I am surprised that compiles. I didn't think members could be initialized like that. I don't think I would ever do it like that though. I would initialize that to
nullptr
and assign its value in a method, possibly the constructor.
We can't tell you why your program exits with 11 because we can not see the code that you are running. We see some code for node and the other class but nothing that has an instance of the class.
BTW - it is a really, really bad idea to use literal values like 10, 11, and 20. I see no reason those should even be there. You should traverse lists until they end, not for a predetermined number. If you want to put 10 values in then loop from 0 to 10. The value to be saved can be based on the loop index but don't index the loop based on those values. Also - the word is spelled vertical.
I think you should make an add method and you should use that instead of what you are doing. Here is what that might look like with a list that acts like a stack.
class VerticalList
{
public:
VerticalList() {}
void AddValue( int value )
{
Node * pnode = new Node( value, m_phead );
m_phead = pnode;
}
private:
Node * m_phead = nullptr;
};
To remove an item you link around the item being removed meaning the node that had the one being removed as its next, now has the removed item's next item as its next item. That means you need to traverse the list and find that item to maintain the list's links. For example if your list is a->b->c->null when you remove b then the list is a->c->null. Item b was the next item for a so a's next is now c.
To copy a list linked like this, you have to first find the tail of the list and add the items in reverse order so the head item is added last.
To put ten values in the list, starting with 11 you can do this :
VerticalList vl;
for( int n = 0; n < 10; ++n )
{
vl.AddValue( n + 11 );
}
If you copy a list by traversing from head to tail it will be in reverse order from the original. If you copy it again then it will be back to its original order since it would have been reversed twice.