-
Notifications
You must be signed in to change notification settings - Fork 66
/
listIterator.h
70 lines (62 loc) · 2.05 KB
/
listIterator.h
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
#include "listNode.h"
#include "iterator.h"
namespace EasySTL {
//迭代器本身不是指针,因为list不是连续的区间
template<class T, class Ref, class Ptr>
struct _list_iterator {
typedef _list_iterator<T, T&, T*> iterator; //指向内部元素值得迭代器
typedef _list_iterator<T, Ref, Ptr> self; //指向list节点的迭代器
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef _list_node<T>* link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
link_type node; //普通指针指向节点
_list_iterator(link_type x) : node(x) {}
_list_iterator() {}
_list_iterator(const iterator& x) : node(x.node) {}
bool operator==(const self& x) const {return node == x.node;}
bool operator!=(const self& x) const {return node != x.node;}
//deference
reference operator*() const {return (*node).data;}
pointer operator->() const {return &(operator*());} //???
//迭代器向前移动一个位置(++i)
self& operator++() {
node = node->next;
return *this;
}
//迭代器向前移动一个位置(i++)
self operator++(int) {
self tmp = *this;
++*this;
return tmp;
}
//对迭代器递减1 (--i)
self& operator--() {
node = node->prev;
return *this;
}
//对迭代器递减1 (i--)
self operator--(int) {
self tmp = *this;
--*this;
return tmp;
}
self operator+(int dis){
self tmp = *this;
while(dis-- > 0) {
tmp = tmp.node->next;
}
return tmp;
}
self operator-(int dis){
self tmp = *this;
while(dis-- > 0) {
tmp = tmp.node->prev;
}
return tmp;
}
};
}