linked list & a copy constructor?

I'm trying to do deep copy for my constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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.
¿Don't you have an insert function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void List::insert(const T &value, node *p){
   node *after = p->next;
   p->next = new node(value, after, p);
   after->prev = p->next;
}

list::list(const list &b): 
   root(&root, &root) //points to itself
{
   node *p = b.root->next;
   while( p not_eq b.root ){
      insert(p->data, this->root.prev);
      p = p->next;
   }
}


I really recommend to use a header cell, and make the list circular. It simplifies the code as you never reach an invalid pointer.
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.
Because you'll probably need the `insert()' function on its own
Because it improves readability
Because it's easier to maintain


You never initialize `_pFront' or `_pBack'
Also, follow the loop ¿where does `_pFront' ends?
@ne555,
thanks for helping. I should include the entire code to let you understand what _pFront means. Anyway, I gave up.
Topic archived. No new replies allowed.