Sorting Linked List in C or C++ in order
void sort (){ node* forth = NULL; for (node* i = head; i->link != nullptr; i = i->link ){ for (node* j = i->link; j != nullptr; j = j->link){ if ( i->data > j->data ){ forth = i; i = j->link; j->link = forth; } } } }
i have that but it is not working any help would be appreciable
Your code doesn't make sense. If you are at the 3rd element, and you decide you need to swap it with the 7th element: The 2nd element must now point at the 7th. The 6th element must now point at the 3rd. The 3rd element must now point at the 8th. The 7th element must now point at the 4th.
Create a function that allows you to swap elements, then after that you can do sorting this way.
A much better way is to create an array, put the linked list items in the array, sort that array, then change all the links based on that array.
i am not suppose to use arrays for the assignment
Then you need to understand how to use linked lists properly.
new to linked list. still trying to grasp the whole concept of linked lists
draw a picture with all the links.
Join our real-time social learning platform and learn together with your friends!