如何解决代码中的错误并解释错误消息

时间:2014-07-07 00:41:39

标签: c++ eclipse stack-dump

所以我正在使用链接列表进行项目。该程序基本上输入数字,直到输入-999,然后对其进行操作。我遇到问题的两个操作是deleteAll,其中所有相同的值都从列表中删除,而deleteSmallest则删除列表中的最小值。调用这两个函数的代码如下:

    int num;
    cout << "Enter a number you would like deleted from the whole list: ";
    cin >> num;

    uList.deleteAll(num);
    cout << "New list: " << uList << endl;

    uList.deleteSmallest();
    cout << "After deleting the smallest number, the list now is: " << uList << endl;

deleteAll的代码如下:

    template <class Type>
    void UnorderedLinkedList<Type>::deleteAll(const Type& deleteItem)
    {
        NodeType<Type>* curr;
        NodeType<Type>* p = NULL;
        NodeType<Type>* q = NULL;

        curr = first;

        if(first == NULL)
            throw std::runtime_error("Cannot delete from an empty list");
        else
        {
            for(int i = 0; i < count; i++)
            {
                if(curr->info == deleteItem)
                {
                    p = curr;
                    q = p->link;
                    p->link = q->link;
                    delete q;
                }
                curr = curr->link;
            }
        }
        delete p;
        delete curr;
    }

deleteSmallest的代码如下:

    template <class Type>
    void UnorderedLinkedList<Type>::deleteSmallest()
    {
        NodeType<Type>* curr;
        NodeType<Type>* p;
        NodeType<Type>* q;
        NodeType<Type>* r;

        curr = first;

        if (first == NULL)
            throw std::runtime_error("Cannot delete from an empty list");
        else
        {
            for(int i = 0; i < count; i++)
            {
                if(curr->link->info < curr->info)
                {
                    int smallest = curr->link->info;
                    p = curr;
                    q = curr->link;
                }
                curr = curr->link;
            }
        }
        r = q->link;
        p->link = q->link;
        delete q;
    }

我得到的错误是:

    1 [main] Project 5 4044 cygwin_exception::open_stackdumpfile: Dumping stack trace to Project 5.exe.stackdump

很抱歉发布一个大问题,但是有人可以解释这种情况下错误意味着什么以及我正在做什么导致这个问题出现?谢谢!

1 个答案:

答案 0 :(得分:0)

对于deleteAll(),你应该这样做:

else
{
    for (Node* prev = curr; curr != NULL; prev = curr, curr = curr->link)
    {
        if (curr->info == deleteItem)
        {
            NodeType<Type>* temp = curr;
            curr = curr->link;

            if (prev)
                prev->next = curr;
            delete temp;
        }
    }
}

之前的方式并没有完全删除curr。您还应该删除底部的delete pdelete curr,因为它们是多余的。

对于deleteSmallest(),你需要保持指向最小节点的指针(以及指向前面节点的前一个节点),这样当循环结束时你知道删除了什么:

else
{
    Node* prev = NULL, **smallest;

    for (Node** curr = smallest = head, *back(*head); *curr != NULL; back = *curr, curr = &(*curr)->link)
    {
        if ((*curr)->info < (*smallest)->info)
        {
            prev = back;
            smallest = curr;
        }
    }

    Node* temp = *smallest;
    *smallest = (*smallest)->link;

    if (prev)
        prev->link = *smallest;
    delete temp;
}