Hi there;
if I have a link list and I want to get the smallest item. How to make that algorithm.
Printable View
Hi there;
if I have a link list and I want to get the smallest item. How to make that algorithm.
If the list is sorted, just pick either the tail or the head depending on how you sorted it.
If it's unsorted, you have to use a linear search (i.e. just go through each node and check).
this is the point:
how to check, using strcmp is enough or should I use any other way to get the smallest item.
note: the list is not sorted
Depends what's in the list. If you have a list of numbers, then this will work:How you decide whether another type of item is smallest is up to you.Code:std::list<int> blah;
// add numbers to blah
int x = std::min(blah.begin(), blah.end());
What are you storing?