mardi 6 octobre 2020

How to properly delete a node by data information in a linked list in c++?

I am asking a user to input a CourseID, then try to find matching by going through whole linked list and once i found I am trying to delete it

struct CourseData {
    string CourseID, MaxCapacity, CurrentCapacity ;
    string CourseName, InstructorName, CourseSection, CourseLocation;
    vector <string> ListOfStudents;
};

ostream& operator<<(ostream &os, const CourseData &data) {
    os << "("<<data.CourseName << ")";  
    return os;
}

class CourseNode {
    private:
        CourseData elem;
        //
        CourseNode* next;

    public:
        CourseNode(CourseData elem) : elem(elem)
        {}  
        friend class Courses ;

};

class Courses {
    private:
        
        CourseNode *head;
    public:     
        Courses();
        ~Courses();
        void deleteCourse();


void Courses::deleteCourse()
{   
    
    CourseNode *ptr=head;
    CourseData a;
    string course;
        
    cin>>course;
    cout<<course<<endl;
    if (head == NULL)
   {
       cout<<"Test";
      return;   
   }

    while(ptr!=NULL)
    {   
        if (ptr->elem.CourseID==course) {
            CourseNode *todel = ptr->next;
            ptr->next = ptr->next->next;
            delete todel;
     }
    ptr=ptr->next;
    }


}

What can be wrong in my delete function, how to properly code this method! Any help will be appreciated

Aucun commentaire:

Enregistrer un commentaire