I have made a LinkedList
class. This is a singly-linked-list and I want to make a forward_iterator
for this class without using Boost. I have made the code and I want to know whether I have implemented it correctly. The source I referred to make this code is here.
template <class T>
struct node
{
T data;
node *next;
};
template <class T>
class LinkedList
{
private :
node<T> *start;
unsigned int numElements;
// Assume all functions are implemented
};
Iterator Code :
class iterator : public std::iterator<std::forward_iterator_tag,node<T>*>
{
node<T>* itr;
public :
iterator (node<T>* temp) : itr(temp) {}
iterator (const iterator& myitr) : itr(myitr.itr) {}
iterator& operator++
{
itr = itr->next;
return *this;
}
bool operator== (const iterator& rhs)
{
return itr == rhs.itr;
}
bool operator!= (const iterator& rhs)
{
return itr != rhs.itr;
}
T& operator*()
{
return itr->data;
}
};
Is the above implementation correct? If not, then what changes should I make? Also, does anything else need to be implemented?