trying to build linked list that sort it selft. but this code below doesnt work. this part of code should insert a node in order. for ex.
if i have a linked list of age 1 3 4 5. and if i insert(3) than result should be 1 2 3 3 4 5. but my code output 1 2 3 4 5 3 :(
if(cur_node->age < ag && cur_node->next->age > ag)
if i have linked list of age1, 6, 7. and if i insert(2). it works fine. because 2 < 1 && 2 is > 6.
problem is if i have age1 2 4 5. and if i insert(2). it does not work bc 2 < 1 && 2 > 2. error here. can idea how can i fix the if statement.
also i allready tryied
if(cur_node->age < ag && cur_node->next->age >= ag)
if(cur_node->age <= ag && cur_node->next->age >= ag)
Try this: Instead of checking both the current node and the next node, just look at the current node. If the element you're considering is less than the current node, insert it before the current node. If it's greater or equal, move on to the next node. If you hit the end of the list without finding a place for it, just put it at the end.