Looks fine.
ThereThere is definitely a bug in your constructor where you build two Sentinels
it should only be one. Otherwise your iterators for an empty list will iterate once.
template<typename T>
typename Linked_List<T>::Iterator Linked_List<T>::begin() const
{
return Iterator(_head->next);
}
template<typename T>
typename Linked_List<T>::Iterator Linked_List<T>::end() const
{
return Iterator(_tail);
}
I haven't yet figured out how to implement:
Iterator insert(const Iterator, T);
Iterator erase(const Iterator);
If you have to insert
before the iterator? Then you can simply implement like I did above:
Iterator insert(const Iterator iterator, T value) {
Node* n = new Node(value, iterator->_current);
return Iterator(n);
}
Lets assume erase returns the iterator to the next element.
Iterator erase(const Iterator iterator)
Node* current = iterator->_current;
if (current == _tail) // can't delete the tail
return iterator;
}
// otherwise unlink from previous item.
if (current->prev == nullptr) {
head = current->next;
}
else {
current->prev->net = current->next;
}
// Next unlink from the next item.
current->next->prev=current->prev;
// Get the next item so we can return it.
Node* result = current->next;
// Delete the old value.
delete current;
// return the new result.
return Iterator(result);
}