Link List Sorting Method
I can't sort node based upon there value please help me to sort nodes on the basis of the Nodes value in ascending order
1 2 3 4 5 6 7 8 9
|
if(Current_Node->get_value() < Last_Current_Node->get_value())
{
temp = Current_Node->get_next();
Current_Node->set_next(Last_Current_Node);
Last_Current_Node->set_next(temp);
}
|
Your linked list looks like;
Where L is Last_Current and C is Current nodes, X and Y some other nodes.
After your swap your nodes, your list looks like:
Because you didn't change node which had pointed to last node before...
Last edited on
Please give me a code example
1 2 3 4 5 6
|
if(Current_Node->get_value() < Last_Current_Node->get_value()) {
temp = Current_Node->get_next();
Current_Node->set_next(Last_Current_Node);
Last_Current_Node->set_next(temp);
Node_before_Last->set_next(Current_Node);
}
|
Of course, you should define Node_before_Last before...
Last edited on
Topic archived. No new replies allowed.