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