Doubly Linked List iterator

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
DLinkedList<T>::Iterator::Iterator(DLinkedList<T> *pList, bool begin) 
{
    this->pList=pList;
    if (begin == true)
    {
        if (pList != NULL) {
            index = 0;
            current = pList->head;
        }
        else {
            index = -1;
            current = NULL;
        }
    }
    else {
        current = NULL;
        if (pList != NULL) 
            index = pList->count;
        else
            index=0;
    }
}

template <class T>
typename DLinkedList<T>::Iterator& DLinkedList<T>::Iterator::operator=(const DLinkedList<T>::Iterator &iterator)
{
    this->current = iterator.current;
    this->index = iterator.index;
    this->pList = iterator.pList;
}

template <class T> 
void DLinkedList<T>::Iterator::set(const T &e)
{
    if (current == NULL)
        throw out_of_range("Segmentation fault!");
    current->data = e;
}

template<class T>
T& DLinkedList<T>::Iterator::operator*() 
{
    if (current == NULL)
        throw out_of_range("Segmentation fault!");
    else
        return current->data;
}

template<class T>
void DLinkedList<T>::Iterator::remove() 
{
    //this is the remove Iterator func 
    
    
}

template<class T>
bool DLinkedList<T>::Iterator::operator!=(const DLinkedList::Iterator &iterator) 
{
    return (this->current != iterator.current || this->index != iterator.index);
}

template<class T>
typename DLinkedList<T>::Iterator& DLinkedList<T>::Iterator::operator++() 
{
    if (current == NULL)
        throw out_of_range("Segmentation fault!");
    current=current->next;
    index++;
    return *this;
}

template<class T>
typename DLinkedList<T>::Iterator DLinkedList<T>::Iterator::operator++(int) 
{
    if (current == NULL)
        throw out_of_range("Segmentation fault!");
    Iterator tmp = *this;
    ++*this;
    return tmp;
}

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.
Last edited on
I know it is already have the remove iterator but in my exercise i have to define one of this only question ( the to do one )
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.
Topic archived. No new replies allowed.