C++从链接列表中删除项目的问题

问题描述 投票:0回答:1

上一个问题解决了!

新问题:代码本身。我一直在研究一个函数的代码,以删除一个链接列表中的选定项目,它可能会出现多少次。但是,当我运行这部分程序试图删除一个节点时,它就会直接结束程序。我知道一定会有什么问题,但即使看了这方面的教程,我也不太明白如何实现这个指令。如果有任何帮助,将非常感谢。[已解决]

功能

void LinkedList::deleteItem(int _newItem)
{
if(isEmptyList())
    cout << "\t<ERROR> List is empty.\n";

else
{
    bool itemDelete = false;

    nodeType *q = first;

    while(q != NULL)
    {
        if(first->info == _newItem)
        {
            nodeType *p = first->link;
            first->link = p->link;
            delete p;
            --count;
            itemDelete = true;
        }

        if(q->link->info == _newItem)
        {
            nodeType *r = q;
            nodeType *p = q;
            r = r->link;
            p->link = r->link;
            delete r;
            --count;
            itemDelete = true;
        }

        q = q->link;
    }
    if(itemDelete == true)
        cout << "Item was deleted.";
    else
        cout << "Item was not found.";
}

}

类和结构

    struct nodeType
    {
     int info;
     nodeType *link;
    };

    class LinkedList
    {
     public:
        void initializeList();
        bool isEmptyList();
        void printList();
        int findLength();
        void destroyList();
        int infoFirst();
        int infoLast();
        bool searchItem(int);
        void insertFront(int);
        void insertBack(int);
        void deleteItem(int);
        int calcTotal();
        int calcAvg();
        LinkedList();

    private:
        nodeType *first, *last, *newNode;
        int count; //adds or remove one whenever a node is added or removed
    };
c++ class struct makefile linked-list
1个回答
0
投票

你的代码有一个问题就是这一行。

if(q->link->info == _newItem)

你正在访问链接列表中的下一个链接 但下一个链接可能是nullptr:

if(q->link != nullptr && q->link->info == _newItem)

在你访问它的成员信息之前,你应该先检查它是否是nullptr.

你也应该考虑将它移到循环之外。

        if(first->info == _newItem)
        {
            nodeType *p = first->link;
            first->link = p->link;
            delete p;
            --count;
            itemDelete = true;
        }

在循环之外,因为每次进入循环时,如果你要删除的节点不是第一个节点,你就会进行这个检查,因为你是先检查节点,而不是检查你用来循环链接列表的节点q。

另外请考虑使用nullptr而不是NULL。

else
{
    bool itemDelete = false;

    nodeType *q = first;

    if(first->info == _newItem)
    {
        nodeType *p = first->link;
        first->link = p->link;
        delete p;
        --count;
        itemDelete = true;
    }
// by doing !itemDelete you will exit once you find the item
// or you won't enter the loop if the item was the first in the list
    while(q != nullptr && !itemDelete)
    {
        if(q->link != nullptr && q->link->info == _newItem)
        {
            nodeType *r = q;
            nodeType *p = q;
            r = r->link;
            p->link = r->link;
            delete r;
            --count;
            itemDelete = true;
        }

        q = q->link;
    }
    if(itemDelete == true)
        cout << "Item was deleted.";
    else
        cout << "Item was not found.";
}

这里的这部分内容可以进一步折叠。

// by doing !itemDelete you will exit once you find the item
while(q != nullptr && !itemDelete)
{
    if(q->link != nullptr && q->link->info == _newItem)
    {
        nodeType *r = q;
        nodeType *p = q;
        r = r->link;
        p->link = r->link;
        delete r;
        --count;
        itemDelete = true;
    }

    q = q->link;
}

to:

 // by doing !itemDelete you will exit once you find the item
    while(q->link != nullptr && !itemDelete)
    {
        if(q->link->info == _newItem)
        {
            nodeType *r = q;
            nodeType *p = q;
            r = r->link;
            p->link = r->link;
            delete r;
            --count;
            itemDelete = true;
        }

        q = q->link;
    }

希望对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.