简体   繁体   中英

Deleting a specific node in Linked List C++

I have a Student class which holds student data and my node is then made up of an object of this student class.

I would like to delete students with marks less then 550.I have a long list of students but the program deletes all data after finding out the first person with less marks.

Here is my linked List:

class LinkList // Linked List 
{
    struct Node // structure for Node containing Object of Student class
    {
        Student info;
        Node* link;
    };
    Node* head;
    
public:
    
    LinkList() // default constructor
    {
        head = NULL;
    }
    
    void CreateEntry();
    void print();
    void Delete();

};

Here is a sample of my data: (One line is each node)

S#  Roll_no     Name         marks
1   0777    Sherlock Holmes  777       
2   0789    John Watson      734       
3   0860    James moriarty   390       
4   0884    Irene Adler      732      
5   1003    Mycroft Holmes   410 
6   1004    Lord Blackwood   632      

Here is my delete function:

Node* ptr = head;
    int n;
    
    while(ptr->link != NULL) // traversing to last element
    {
        n = ptr->info.GetSerialNo(); // where n becomes the total number of elements
        ptr = ptr->link;
    }

    Node* temp = head;
    for(int i=0; i<n; i++)
    {
        if(temp->info.Getmarks() < 550) // Getmarks returns the marks
        {
            if(temp != NULL && temp->link != NULL)
            {
                Node* nodeToDelete = temp->link;
                temp->link = temp->link->link;
                delete nodeToDelete;
            }   
        }
        else
        {
            temp = temp->link;
        }
    }
}

And the output I get is:

S#  Roll_no     Name         marks
1   0777    Sherlock Holmes  777       
2   0789    John Watson      734

I would like to delete students with marks less then 550

Your Delete() is way more complicated than it needs to be for that task. Also, it has some logic mistakes in it anyway.

Try something more like this instead:

void LinkList::Delete()
{
    Node* ptr = head;
    Node* previous = NULL;
    
    while (ptr != NULL)
    {
        Node* nextNode = ptr->link;

        if (ptr->info.Getmarks() < 550)
        {
            if (previous)
                previous->link = nextNode;

            if (head == ptr)
                head = nextNode;

            delete ptr;
        }
        else
            previous = ptr;

        ptr = nextNode;
    }
}

Alternatively:

void LinkList::Delete()
{
    Node* ptr = head;
    Node** previous = &head;
    
    while (ptr != NULL)
    {
        Node* nextNode = ptr->link;

        if (ptr->info.Getmarks() < 550)
        {
            *previous = nextNode;
            delete ptr;
        }
        else
            previous = &(ptr->next);

        ptr = nextNode;
    }
}

That being said, you really should use the standard std::list container instead of making your own linked list, then you can do this:

#include <list>

class LinkList // Linked List 
{
    std::list<Student> students;
    
public:
    
    LinkList() = default; // default constructor
    
    ...
    void Delete();

};
#include <algorithm>

void LinkList::Delete()
{
    students.erase(
        std::remove_if(students.begin(), students.end(),
            [](Student &s){ return s.Getmarks() < 550; }
        ),
        students.end()
    );
}

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