PDA

Click to See Complete Forum and Search --> : Smallest iteam using Link list


ubarcomp
Oct 20th, 2002, 03:19 AM
Hi there;

if I have a link list and I want to get the smallest item. How to make that algorithm.

parksie
Oct 20th, 2002, 03:52 AM
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).

ubarcomp
Oct 20th, 2002, 08:13 AM
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

parksie
Oct 20th, 2002, 08:15 AM
Depends what's in the list. If you have a list of numbers, then this will work:std::list<int> blah;

// add numbers to blah

int x = std::min(blah.begin(), blah.end());How you decide whether another type of item is smallest is up to you.

What are you storing?