Thank you so much! the contained linked lists (of char *) are already sorted, I'm not sure which sorting algorithm I have to use, it doesn't matter I guess.
I have to sort the "big" linked list alphabetically according to the first element in each contained list.
hope I'm clear enough
thank you again
for example
car rac
dog god
bed
will become :
bed
car rac
dog god
(actually this is a final step of a big problem about finding anagrams and sorting them, I did all the work this part is still missing)
Is your "small list" a different list-type (class) than your "big list"?
If so, you could simply overload operator<() and use std::sort to do the hard work:
http://www.cplusplus.com/reference/algorithm/sort/
If you are required to do it "manually", for linked lists i think bubble sort is probably the easiest to implement because it just traverses the list. A very good explanation on bubble sort is available here:
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/
In short:
- Loop through the big list, if the first element of the child is smaller than the next one, swap the two.
- keep doing this until the list is sorted (no swaps were made).
Please do come back to us with some of your code if you require any further assistance.
Given you have a linked list of linked lists, you first need to lexicographically sort the strings in the linked lists, and then lexicographically sort the lists in the linked list based on their first element.
Suppose that I have a list of lists L, and that the elements of each L[i] are sorted. Then, to sort L, I would iterate it, comparing against L[i][0]. Does that make more sense?
Here is some pseudo code:
1 2 3 4 5 6 7
do
has_swapped = falsefor i from 0 to L.size - 1,
if L[i][0] > L[i + 1][0]
swap(L[i][0], L[i + 1][0])
has_swapped = truewhile has_swapped
If I were you, I would add a (re) sort method to my linked list class that takes a comparer lambda expression; this would make it even more general, as I could then kill many birds with one stone.