Home > Back-end >  How to delete a specific node in a linked list?
How to delete a specific node in a linked list?

Time:10-01

I have the next linked list code:

#include <iostream>
#include <string>
using namespace std;
#define null 0

class Node
{
public:
    string name;
    int age;
    Node *next;
    Node(string name, int age)
    {
        this->age = age;
        this->name = name;
        this->next = null;
    }
};

class LinkedList
{
private:
    Node *head;
public:
    LinkedList()
    {
        this->head = null;
    }

    void insert(string name, int age)
    {
        Node *nodenew = new Node(name, age);
        nodenew->next = null;
        if (this->head == null)
        {
            this->head = nodenew;
        }
        else
        {
            Node *temp = this->head;
            while (temp->next != null)
            {
                temp = temp->next;
            }
            temp->next = nodenew;
        }
    }

    void print()
    {
        if (this->head == null)
        {
            cout << "Empty";
        }

        Node *temp = this->head;

        while (temp != null)
        {
            cout << temp->name << ", " << temp->age <<endl;
            temp = temp->next;
        }
    }

    void deletePerson(string name){
        Node *temp = this->head;
        Node* prev = null;

        if (temp!=null && temp->name == name)
        {
            this->head = temp->next;
            delete temp;
            return;
        }else
        {
            while (temp != null && temp->name == name)
            {
                prev = temp;
                temp = temp->next;
            }
            if (temp == null)
            {
                return;
            }
            prev->next = temp->next;
            delete temp;     
        } 
    }


};

int main(int argc, char const *argv[])
{
    LinkedList list;
    list.insert("David", 45);
    list.insert("John", 23);
    list.insert("Katty", 78);
    list.insert("Stephanie", 25);
    list.deletePerson("Katty");
    list.print();
    return 0;
}

I am trying to delete a node by a given name with the next method:

void deletePerson(string name){
        Node *temp = this->head;
        Node* prev = null;

        if (temp!=null && temp->name == name)
        {
            this->head = temp->next;
            delete temp;
            return;
        }else
        {
            while (temp != null && temp->name == name)
            {
                prev = temp;
                temp = temp->next;
            }
            if (temp == null)
            {
                return;
            }
            prev->next = temp->next;
            delete temp;     
        } 
    }

But when I compile my code it doesn´t show me anything in console, it just show the next code error:

code=3221225477

I tried to debugger my code but vscode just swap me to other class, in prev->next = temp->next; part it just show a little message that says:

Exception has occurred. X
Segmentation fault

My expected output would be:

David, 45
John, 23
Stephanie, 25

I hope you can help me to solve that, thanks.

CodePudding user response:

your problem is so simple, it's in that line:

while (temp != null && temp->name == name)

it should be:

while (temp != null && temp->name != name)

as the condition of looping, you are looping until you either didn't reach the end (temp != null) or the name isn't found in this iteration (temp->name != name).

also, there is a small warning at this line:

prev->next = temp->next;

imagine if you didn't find the node, then temp will be null, and the line temp->next; will throw an error as you are trying to dereference a null pointer.

I know you handled this situation in your code as you wrote:

if (temp == null)
{
     return;
}

but for safety reasons or in case your compiler complained about it as mine, you should check if both prev and temp are null pointers or not, so it should be:

if(null !=  prev && null != temp)
     prev->next = temp->next;

after this edited, this is the output:

David, 45
John, 23
Stephanie, 25
  • Related