Hi,
I have a single link list which is sorted.
structure of which is like
typedef struct mylist
{
int num;
struct mylist *next;
} MYLIST;
How can I perform binary search on this link list. or should I perform
search on right from the beginning.
Not really a C question (probably better asked in comp.programming),
but:
<OT>
Efficient binary search requires that you can quickly access any element
(usually by its index), so it's more naturally suited to arrays. If,
however, you're forced to use linked list (and you say it's sorted),
yuo have two ways of approaching this. One is to copy the whole list
into an array and then perform binary search there (not memory
efficient). The other would be to keep everything in the linked list,
and traverse it from the beginning to the required element whenever you
need to test one. In terms of speed, I'd expect the first to be
quicker, as you traverse the whole list only once, although copies may
foul this up. The speed of the second approach depends on the list
contents and exactly which element you're after.
</OT>
--
BR, Vladimir
Sometimes I live in the country,
And sometimes I live in town.
And sometimes I have a great notion,
To jump in the river and drown.