2014-04-07 3 views
0

순환 링크 된 목록 코드에있는 논리적 오류를 지적하고 설명해 줄 수있는 사람이 있습니까? 미리 감사드립니다.C++ 순환 링크 목록 오류

template <class xtype> 
void clist<xtype>:: copylist (const clist<xtype> & other) 
{ 
    node<xtype> *temp; 
    node<xtype> *p; 

    if (head !=NULL) 
     makeEmpty(); 
    if (other.head == NULL) 
     head = NULL; 
    else 
    { 
     p = other.head; 
     head = new node<xtype>; 

     head->info = p->info; 
     temp = head; 

     p = p->next; 

     while(p != head) 
     { 
      temp->next = new node<xtype>; 
      temp = temp->next; 
      temp->info = p->info; 
      p = p->next; 
     } 
     temp->next = head; 
    } 
} 

답변

0

코드에서 그냥 빨리보고, 나는이 자리 : 당신은 other.head하지 head을 테스트해야

while(p != head) 

.

+0

왜 머리 대신 other.head를 사용합니까? – user3504938

+0

@ user3504938'p'가'other'리스트를 반복하고 있기 때문입니다. 'head' 자체가 당신이 생성하는 목록에 있고,'other'를 가로 지르면 결코 만나지 않아야합니다. – paddy