ok the sort tecnique is not important now,the thing i have to is to
sort the nodes,and i dont expect a code only a way
That's what your textbook and class notes are for. But it involves two
things: traversing the list somehow, and swapping nodes that are out of
order.
For something like a bubble sort, you effectively traverse the list multiple
times, using nested loops. Something like this:
for each node I in the last _except_ the last one:
for each node J _after_ I in the list:
if the data value in I is greater than the data value in J, then:
swap nodes I and J
(For descending sort, change "greater than" to "less than".)
So you need to know how to swap in a singly linked list. Do you? It
requires four pointers: one for each node to be swapped, and one for each of
the nodes _preceding_ the nodes to be swapped (because you need to modify
their "next" pointers, right?). NOTE that if your node I in the algorithm
above is the head node, then when you go to swap it, there IS NO preceding
node! In that case, you'll need to identify that special case and modify
the "head" pointer instead.
Work it out on paper, if your textbook or class notes don't contain the
details. Then turn the design into some real code.
-Howard