#Issues with my custom "Circular Singly Linked List" Iterator

24 messages · Page 1 of 1 (latest)

dense compass
#

Hello, I'm making a custom Circular Singly Linked List and I'm having issue making the custom iterator.
If I want the loop the go from the head to the tail : the begin() should be an Iterator( m_pHead ) and the end() an Iterator( m_pTail->m_pNext ) because I don't want to stop when the iterator reaches the tail but when it reaches the node after the tail (so that the code in the "for" is also applied to the tail).
The issue is that m_pHead == m_pTail->m_pNext so the loop doesn't event start because the iterator is already equal to m_pTail->mèpNext

arctic spearBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dense compass
#

Does anyone have an idea ?

quasi kiln
#

Your end iterator should be a sentinel iterator, not iterator(tail ->next)

dense compass
#

sentinel ?

quasi kiln
#

Special value iterator. Like sometimes -1 is used as a sentinel value to indicate an index that doesn't exist, e.g. for finding a value in a vector

dense compass
#

but then my loop iterator is never going to be the end iterator if it's not a value that is in the list ?

#

like the

template <typename T>
typename ITERATOR& ITERATOR::operator++()
{
    m_pNode = m_pNode->m_pNext;
    return *this;
}
#

it only select the next node

#

how is it going to be able to compare ?

quasi kiln
#

You need to figure out how to make that not happen by returning a sentinel iterator instead of the begin iterator

dense compass
#

I'm sorry but I tried to search online how to make a sentinel, examples of existing ones but I really have no idea how to do it...

quasi kiln
#

Make m_pNode null, for example

tribal kindle
#

and that special node can point back to the head if you want

#

then you can use that special "sentinel" node to mark that you reached the end

dense compass
#

oh alr

#

ooooooh

#

ok I'll try it

dense compass
#

alr so it worked perfectly by adding the "sentinel" Node between tail and head

#

but I'm gonna try something else that I though of because I don't really like the fact that I have to take more memory in my list simply for an iterator that is not even necessary (only useful for the for ( auto it : list ) in my case)

#

but thanks !

#

😄

#

!solve