C ++循环链表错误

时间:2014-04-07 01:39:47

标签: c++11 circular-list

任何人都可以帮我指出并解释我在这个循环链表代码中的逻辑错误吗?提前谢谢。

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;  
    }  
}  

1 个答案:

答案 0 :(得分:0)

快速查看您的代码,我发现了这一点:

while(p != head)

您应该测试other.head,而不是head