suppose we have doubly linked list ordered by integer value:
struct ListItem
{
int value;
ListItem *prev, *next;
};
struct List
{
ListItem *first, *last;
int count;
};
can we use faster search algorithm such as binary search to locate ListItem inside List and how?
For most practical purposes, no. If you want faster search, a linked list is a poor choice of data structure. Consider a vector, deque, set, or multiset instead.
Edit: Perhaps it would be good to provide some guidance about which of those makes sense when. A vector makes the most sense if you have two basically separate phases: Either you insert all your data in order, or you insert and sort, then after the data is sorted, the data remains static, and you just search in it. A deque is pretty much the same, except you can insert at either end, so if you might get data out of order, but new data always belongs at one end of the collection or the other, it can be a good choice.
A
setormultisetworks better if you’re going to be mixing insertions/deletions with lookups. It stays sorted all the time, so searches are always reasonably fast. Between the two (set vs. multiset) the choice is pretty simple: if you need to ensure each item in the collection is unique, you want set. If you might have more than one item with the same key, you want a multiset.