简体   繁体   English

使用链接列表时出现EXC_BAD_ACCESS错误

[英]EXC_BAD_ACCESS Error when using linked list

My program still compiles and prints from the inputted data files. 我的程序仍然可以从输入的数据文件中进行编译和打印。 However, an error still occurs after everything has printed and so the program doesn't end cleanly. 但是,打印完所有内容后仍然会发生错误,因此程序无法完全结束。

I get the error specifically in this part of my program 我在程序的这一部分中特别收到错误

while (current->next != tail)

Basically this program is using a linked list to store information and outputting it into the screen. 基本上,该程序使用链接列表来存储信息并将其输出到屏幕中。 My particular error is with the clear() function that is supposed to clear the entire linked list with the pop_back() function. 我的特别错误是clear()函数,该函数应该使用pop_back()函数清除整个链接列表。

//removes the last object from the linked list – deallocates memory
template <typename T>
void LL<T>::pop_back()
{

    if(count==0)
    {
        //Do nothing. Nothing to remove.
        return;
    }
    else{
        Node<T> *current;
        current=head;

        while (current->next != tail)
        {
            current=current->next;

        }

        delete tail;
        tail=current;

        count--;
    }
}

//Clears the linked list
template <typename T>
void LL<T>::clear()
{
    Node<T> *current= head;


    while (current != NULL)
    {
    pop_back();
    //current=tail;
}
current=tail;

head=tail=NULL;

}

Any help would be much appreciated. 任何帮助将非常感激。

Your pop_back method doesn't handle the case where the tail and the head are the same element (aka a linked list of 1 element). 您的pop_back方法无法处理尾巴和头部是同一元素(也就是1个元素的链表)的情况。 As a quick fix maybe an extra check for that case? 作为快速解决方案,可能需要对这种情况进行额外检查?

if(count==0)
{
    //Do nothing. Nothing to remove.
    return;
}
else if (count==1)
{
    head = tail = NULL;
    count--;
    return;
}

Also this loop is infinite as written: 同样,这个循环是无限的:

while (current != NULL)
{
    pop_back();
    //current=tail;
}

maybe while (head != NULL) or while (count != 0) ? 也许while (head != NULL)while (count != 0)吗?

You need to update node before the deleted one. 您需要在删除节点之前更新节点。 Here is simplified version of your pop_back() method: 这是pop_back()方法的简化版本:

template <typename T>
void LL<T>::pop_back()
{
    Node<T> curr = head;
    Node<T> prev = NULL;

    while(curr != NULL && curr->next != NULL) // I'm assuming your tail's value is NULL
    {
        prev = curr;
        curr = curr->next;
    }

    if(curr != NULL)
    {
        if(prev != NULL)
            prev->next = NULL;
        delete curr;
        --count;
        if(count == 0)
            head = NULL;
    }
}

I didn't compiled the code, but I think idea is clear. 我没有编译代码,但是我认为想法很明确。

BTW you can improve performance of the clear() method: 顺便说一句,您可以提高clear()方法的性能:

Node<T> curr = head;
while(curr != NULL)
{
    Node<T> tmp = curr->next;
    delete curr;
    curr = tmp;
}

head = NULL;
tail = NULL;
count = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM