Hi everyone, so today I have to deal with doubly linked list and using Iterator in it. I still don't know where i went wrong, please spend sometime help me. Thanks a lot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void DLinkedList<T>::Iterator::remove()
{
/*
* TODO: delete Node in pList which Node* current point to.
* After that, Node* current point to the node before the node just deleted.
* If we remove first node of pList, Node* current point to nullptr.
* Then we use operator ++, Node* current will point to the head of pList.
*/
Node* delN=current;
Node*tmp=delN->next;
current=current->previous;
current->next=tmp;
tmp->previous=current;
delete[] delN;
}
Here are all the other funtions in the same class Iterator I have done, If I did it wrong, please help me figure it out.
Iterators are designed primarily to decouple algorithms from the ranges they operate upon. Algorithms like remove should not be member functions of iterator; this defeats its main purpose.
It's hard to tell without seeing the declaration of DLinkedList<>, but it looks to me like remove() will fail if it deletes the first or last item. You'll access null pointers and it doesn't update the head and tail pointers.