List::List(const List& list)
{
if ( list.empty() )
{
this -> _pFront = 0;
this -> _pBack = 0;
return;
}
if ( list._pFront == list._pBack )
{
ListNode* p = new ListNode( list._pFront ->_data );
this -> _pFront = p;
this -> _pBack = p;
return;
}
ListNode* p1;
ListNode* p = list._pFront ;
do
{
p1 = new ListNode( p ->_data );
_pFront -> _pNext = p1;
p1 -> _pPrev = _pFront;
_pFront = p1;
p = list._pFront->_pPrev;
}while(p);
p1 = new ListNode( p ->_data );
_pBack->_pPrev = p1;
}
The problem in this part
1 2 3 4 5 6 7 8 9 10 11 12 13 14
ListNode* p1;
ListNode* p = list._pFront ;
do
{
p1 = new ListNode( p ->_data );
_pFront -> _pNext = p1;
p1 -> _pPrev = _pFront;
_pFront = p1;
p = list._pFront->_pPrev;
}while(p);
p1 = new ListNode( p ->_data );
_pBack->_pPrev = p1;
I think I'm not really able to track the new node for this reason my app crashes.
ListNode* p = list._pFront ;
this is for the original node
p ->_data;
the data inside the original node.
Each node has three pointers. One for the entire node which tracks whether the node in front or in the back. The other two for next and previous nodes.
I really got confused. In the board, it seems to me perfect. Once I implement it it crashes. Why do I need to implement another function since do-while loop does that.