Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete a specific node in a linked list?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

also, for safety reasons, you should check of 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading