Question

I'm having problem on how to get out of the loop in a doubly linked list when it gets to the end of the deque. If there is an element in the deque, it will return the iter position. If not, it will return the iterator at the end of the deque. I'd really appreciate your help. Thank you

Here is my search function

unique_ptr<DequeIterator<E>> find(E match)
{
    assert(!is_empty());

        // the iter will begin from the head.
    unique_ptr<DequeIterator<E>> iter(iter_begin());

        // Here is where I do not know how to get it quit when 
        // it gets to the end of the deque.
        // ALSO it needs to check the value at the end of
        // the deque before it quits too.
    while(iter->value() != match)
    {
        iter->next();
    }
    return iter;
}
Was it helpful?

Solution

Surely just something like:

while( iter != iter_end() && 
       iter->value() != match )
{
    iter->next();
}

return iter;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top