简体   繁体   English

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

[英]C++ Issue with deleting item from linked list

Previous issue resolved!上一个问题解决了!

New issue: the code itself.新问题:代码本身。 I've been working on the code for a function to delete a selected item off a linked list, how many times it may appear.我一直在研究 function 的代码,以从链接列表中删除选定的项目,它可能会出现多少次。 However, when I run that part of the program to try to delete a node, it'll just end the program.但是,当我运行该部分程序以尝试删除节点时,它只会结束程序。 I know something's bound to be off but even looking at tutorials on this, I'm not quite grasping how to achieve this instruction.我知道肯定会发生一些事情,但是即使查看有关此的教程,我也不太了解如何实现此指令。 Any help would be greatly appreciated.任何帮助将不胜感激。 [RESOLVED] [解决]

Function Function

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

} }

Class and struct Class 和结构

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

One issue with your code is this line:您的代码的一个问题是这一行:

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

you are accessing the next link in your linked list but that next link may be nullptr:您正在访问链接列表中的下一个链接,但下一个链接可能是 nullptr:

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

Before you access its member info you should first check to see if it is nullptr.在访问其成员信息之前,您应该首先检查它是否为 nullptr。

You should also consider moving:您还应该考虑搬家:

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

outside the loop because you will go through this check everytime that you enter the loop if the node that you are trying to remove is not the first one since you are checking the node first, instead of the node q which you are using to loop through your linked list.在循环之外,因为您将在每次进入循环时通过此检查 go 如果您尝试删除的节点不是第一个,因为您首先检查节点,而不是您用来循环的节点 q你的链表。

Also please consider using nullptr instead of NULL.另外请考虑使用 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.";
}

This portion here can be further collapsed:这部分可以进一步折叠:

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

I hope that this helps you.我希望这对你有帮助。

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

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