forked from MouCoder/cpp_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist
More file actions
220 lines (209 loc) · 4.45 KB
/
list
File metadata and controls
220 lines (209 loc) · 4.45 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include<iostream>
#include<list>
#include<assert.h>
#include<vector>
using namespace std;
namespace MyList
{
template<class T>
//模板的使用_list_node<type>
struct __list_node
{
__list_node<T>* prev;//指向后一个节点的指针
__list_node<T>* next;//指向前一个节点的指针
T data;//数据域
//node的默认构造函数,对成员变量进行初始化。T()表示调用默认构造函数,即x会调用默认构造函数
__list_node(const T& x = T())
:prev(nullptr)
, next(nullptr)
, data(x)
{}
};
//list迭代器的实现
//Ref->T& ptr->T*
template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef __list_node<T> node;
typedef __list_iterator<T, Ref, Ptr> self;
node* _node;
//迭代器的作用不是创建节点而是指向原有节点,因此使用指针即可
__list_iterator(node* node)
:_node(node)
{}
//list迭代器中不用实现拷贝构造、赋值运算符重载、析构,因为迭代器的本质并不是创建节点,使用默认的进行值拷贝即可。
bool operator!=(const self& s) const //不需要进行修改,设计成const无论是const和非const都可以调用
{
return _node != s._node;
}
bool operator==(const self& s) const
{
return _node == s._node;
}
//重载->
Ptr operator->() const
{
return &_node->data;
}
Ref operator*() const
{
return _node->data;
}
//前置++
self operator++()
{
_node = _node->next;//直接返回++后的结果
return *this;
}
//后置++
self operator++(int) //参数只是为了和前置构成重载,并没有实际意义,因此只给类型就可以了
{
self tmp = *this;
_node = _node->next;
return tmp;
}
//前置--
self operator--()
{
_node = _node->prev;
return *this;
}
//后置--
self operator--(int)
{
self tmp = *this;
_node = _node->prev;
return tmp;
}
};
template<class T>
class list
{
typedef __list_node<T> node;
public:
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
//迭代器
iterator begin()
{
return iterator(head->next);//调用iterator的构造函数,将node*转换成iterator类型
}
iterator end()
{
return iterator(head);
}
const_iterator begin() const
{
return head->next;
}
const_iterator end() const
{
return head;
}
/*构造函数*/
//默认构造
list()
{
head = new node;
//下一个节点指针和前一个节点指针都指向自身的空链表
head->next = head;
head->prev = head;
}
template<class InputIterator>
list(InputIterator first, InputIterator last)
{
head = new node;
head->next =head;
head->prev = head;
//调用push_back插入first到last的节点
while (first != last)
{
push_back(*first);
++first;
}
}
list(const list<T>& lt)
{
head = new node;
head->next = head;
head->prev = head;
list<T> tmp(lt.begin(), lt.end());
std::swap(head, tmp.head);
}
//lt1 = lt2
list<T>& operator=(list<T> lt)
{
//传值会进行临时拷贝,lt就是一个新的list,将lt和this->head交换,函数结束释放空间就将this->head释放而新的this->head=lt.head
std::swap(head, lt.head);
return *this;
}
//析构函数
~list()
{
clear();
delete head;
head = nullptr;
}
/*插入*/
//尾插
void push_back(const T& x)
{
insert(end(),x);
//node* newnode = new node(x);
////插入节点
//node* tail = head->prev;
//tail->next = newnode;
//newnode->next = head;
//newnode->prev = tail;
//head->prev = newnode;
}
void push_front(const T& x)
{
insert(begin(),x);
}
iterator insert(iterator pos,const T& x)
{
node* newnode = new node(x);
node* cur = pos._node;
node* prev = cur->prev;
newnode->next = cur;
newnode->prev = prev;
prev->next = newnode;
cur->prev = newnode;
return iterator(newnode);
}
/*删除*/
void clear()
{
iterator it = begin();
while (it != end())
{
erase(it++);
}
}
iterator erase(iterator pos)
{
assert(pos != end());
node* cur = pos._node;
node* prev = cur->prev;
node* next = cur->next;
next->prev = prev;
prev->next = next;
delete cur;
return iterator(next);
}
void pop_back()
{
assert(head->prev != head);
erase(--end());
}
void pop_fornt()
{
assert(head->prev != head);
erase(begin());
}
private:
//双向带头循环链表
node* head;
};
}