简体   繁体   中英

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. 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

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

    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:

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

Before you access its member info you should first check to see if it is 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.

Also please consider using nullptr instead of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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