-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmy_list.h
More file actions
179 lines (148 loc) · 2.58 KB
/
my_list.h
File metadata and controls
179 lines (148 loc) · 2.58 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once
#include <iostream>
using namespace std;
template <typename T>
struct __ListNode
{
T _val;
__ListNode* _next;
__ListNode* _prev;
__ListNode(const T& val)
: _val(val)
, _next(NULL)
, _prev(NULL)
{}
};
template <typename T, typename Ref, typename Ptr>
class __ListIterator
{
typedef __ListIterator<T, Ref, Ptr> Self;
public:
__ListIterator()
{}
__ListIterator(__ListNode<T>* node)
: _node(node)
{}
Ref operator*()
{
return _node->_val;
}
Ptr operator->()
{
return &(operator*());
}
Self& operator++()
{
_node = _node->_next;
return *this;
}
Self operator++(int)
{
Self tmp(*this);
_node = _node->_next;
return tmp;
}
bool operator==(const Self& it) const
{
return _node == it._node;
}
bool operator!=(const Self& it) const
{
return !(*this == it);
}
public:
__ListNode<T>* _node;
};
template <typename T>
struct List
{
typedef __ListNode<T> Node;
typedef __ListIterator<T, T&, T*> Iterator;
typedef __ListIterator<T, const T&, const T*> ConstIterator;
public:
List()
: _head(BuyNode(T()))
{
_head->_next = _head;
_head->_prev = _head;
}
//void PushBack(const T& val)
//{
// Node* tmp = BuyNode(val);
// Node *tail = _head->_prev;
// tmp->_next = _head;
// tmp->_prev = tail;
// tail->_next = tmp;
// _head->_prev = tmp;
//}
void PushBack(const T& val)
{
Insert(End(), val);
}
void PushFront(const T& val)
{
Insert(Begin(), val);
}
void PopBack()
{
Erase(--End());
}
void PopFront()
{
Erase(Begin());
}
void Insert(const Iterator pos, const T& val)
{
Node* next = pos._node;
Node* prev = next->_prev;
Node* tmp = new Node(val);
tmp->_next = next;
next->_prev = tmp;
tmp->_prev = prev;
prev->_next = tmp;
}
void Erase(const Iterator pos)
{
Node* next = (pos._node)->_next;
Node* prev = (pos._node)->_prev;
prev->_next = next;
next->_prev = prev;
delete pos._node;
}
Iterator Begin()
{
return Iterator(_head->_next);
}
Iterator End()
{
return Iterator(_head);
}
ConstIterator Begin() const
{
return Iterator(_head->_next);
}
ConstIterator End() const
{
return Iterator(_head);
}
protected:
Node* BuyNode(const T& val)
{
Node* tmp = new Node(val);
return tmp;
}
protected:
Node* _head;
};
void TestList()
{
List<int> l;
for (int i = 0; i < 10; i++)
l.PushBack(i);
List<int>::Iterator pos = l.Begin();
while (pos != l.End())
{
cout << *pos << endl;
++pos;
}
}