为什么我的析构函数导致我的链表出现问题

时间:2016-03-11 22:15:22

标签: c++ list linked-list append destructor

我的析构函数:

~list() {
    for (node *p; !isEmpty();) 
    {
        p = head->next;
        delete head;
        head = p;
    }
}

我的功能附加:

void append( list L2)
{

    if (head == 0 && L2.head == 0)
    {
        cout << "Error List Empty" << endl;



    }
    else if (isEmpty())
    {
            L2.printAll();
    }
    else if (L2.isEmpty())
    {
            printAll();  
    }


    else
    {
        tail->next = L2.head;
        tail = L2.tail;
        L2.head = L2.tail = 0;
    }
}

析构函数尝试运行时代码停止。

我的主要人物:

int main()
{
list l1;
l1.addToHead(1);
l1.addToHead(2);

list l2;
l2.addToHead(9);
l2.addToHead(3);
l2.addToHead(4);

l1.printAll();

cout << "-------------------" << endl;
l2.printAll();

l1.append(l2);

cout << "=============" << endl;

输出: 2 1 斜线 4 3 9 平等线 按任意键继续......

当我没有考虑其他条件时,如果l1为空或l2为空,则代码工作正常。它将l2附加到l1,并清空l2。

但是现在我试图将两个列表都清空或一个列表为空,我得到了一个调试断言。看起来析构函数试图运行,然后在#34;删除头部之前遇到问题&#34;

UPDATE -

isEmpty fn:

    int isEmpty() {
    return head == 0;
}

2 个答案:

答案 0 :(得分:0)

我敢打赌,isEmpty正在检查head是否为NULL,而true总是在评估node* p = NULL; while (head != NULL) { p = head->next; delete head; head = p; } ,因为您从未对其进行过设置。

请改为尝试:

<svg width="100%" height="100%" viewBox="0 0 570 594" preserveAspectRatio="true" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="padding: 0 10px 0 18px;fill-rule:evenodd;clip-rule:evenodd;stroke-miterlimit:10;">
    <path id="orbit" d="M146.719,183.637l-18.408,-7.796l-13.233,-6.252l-12.327,-6.302l-18.44,-9.379l-12.42,-11.695l-16.36,-10.421l-15.546,-10.511l-12.326,-12.281l-14.415,-14.728l-8.426,-16.45l-4.168,-14.276l2.084,-14.272l6.297,-11.239l8.019,-10.103l12.013,-6.302l16.682,-8.426l16.356,-4.169l22.804,-4.217l27.474,-4.168l22.03,0l21.75,1.042l24.881,1.042l20.524,1.042l26.875,3.126l27.917,5.211l41.477,9.293l37.047,10.702l41.159,12.782l35.33,14.012l19.808,8.426l25.874,12.554l18.86,11.423l18.578,11.556l18.815,14.105l17.777,16.951l12.233,16.718l8.345,17.187l1.091,27.64l-7.434,8.207l-11.194,10.466l-15.595,10.559l-24.221,7.844l-22.609,5.211l-30.925,3.265l-43.658,0l-32.546,-2.085" style="fill:none;stroke-width:0px;stroke:#ff6060;"/>
    <g id="moon" transform="translate(-53, -130)">
        <path d="M77.39,295.34c0,-10.683 -8.658,-19.343 -19.342,-19.343c-10.683,0 -19.344,8.66 -19.344,19.343c0,10.683 8.661,19.343 19.344,19.343c10.684,0 19.342,-8.66 19.342,-19.343" style="fill:#fff;fill-rule:nonzero;"/>
        <path d="M61.54,304.476c0,-2.967 -2.404,-5.373 -5.371,-5.373c-2.969,0 -5.373,2.406 -5.373,5.373c0,2.967 2.404,5.373 5.373,5.373c2.967,0 5.371,-2.406 5.371,-5.373" style="fill:#878787;fill-opacity:0.199997;fill-rule:nonzero;"/>

        <animateMotion dur="6s" repeatCount="indefinite">
            <mpath xlink:href="#orbit" />
        </animateMotion>
        <animateTransform attributeName="transform"
                      attributeType="XML"
                      type="rotate"
                      from="0 60 70"
                      to="360 60 70"
                      dur="10s"
                      repeatCount="indefinite"/>
    </g>
</svg>

答案 1 :(得分:0)

我认为isEmpty()的返回类型应该是boolean

bool isEmpty() {
    return head == 0;
 }