【STL】一个简单的list实现

list和vector是STL库中两个最基本的容器,以下代码模拟实现一个简单的list:

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#ifndef _MYLIST_
#define _MYLIST_
#include<iostream>
using namespace std;
class ListIsEmpty{};
class ArgIsIll{};
template<typename T>
class Node{
public:
Node(const T& d=T()):data(d),prev(NULL),next(NULL)
{}
~Node()
{}
private:
Node(const Node& n);
Node& operator=(const Node& n);
public:
T data;
Node* prev;
Node* next;
};
template<typename T>
class List{
public:
List():front(NULL),tail(NULL),cur_size(0)
{}
~List()
{
while(!this->empty())
{
this->pop_front();
}
}
List& operator=(const List& l)
{
if(&l!=this)
{
Node<T>* cur=l.front;
List newlist;
while(cur!=NULL)
{
newlist.push_front(cur->data);
}
}
return newlist;
}
Node<T>* begin()
{
return front;
}
Node<T>* end()//此处end函数与STL中稍微有点不同,STL中end返回值指向最后一个元素的下一个,这里指向最后一个元素
{
return tail;
}
bool empty()
{
if(cur_size==0)
return true;
else
return false;
}
size_t size()
{
return cur_size;
}
bool push_front(Node<T>* n)
{
if(this->empty())
{
front=tail=n;
}
else
{
n->next=front;
front->prev=n;
n->prev=tail;
tail->next=n;
front=n;
}
cur_size++;
return true;
}
bool pop_front()throw (ListIsEmpty)
{
if(this->empty())
{
throw ListIsEmpty();
}
else if(this->size()==1)
{
delete front;
front=tail=NULL;
}
else
{
Node<T>* s=front;
front=front->next;
front->prev=tail;
tail->next=front;
delete s;
}
cur_size--;
return true;
}
bool push_front(const T& t)
{
Node<T> *s=new Node<T>(t);
return push_front(s);
}
bool push_back(Node<T>* n)
{
if(this->empty())
{
front=tail=n;
}
else
{
tail->next=n;
n->prev=tail;
n->next=front;
front->prev=n;
tail=n;
}
cur_size++;
return true;
}
bool pop_back()throw (ListIsEmpty)
{
if(this->empty())
{
throw ListIsEmpty();
}
else if(this->size()==1)
{
delete tail;
front=tail=NULL;
}
else
{
Node<T>* s=tail;
tail=tail->prev;
front->prev=tail;
tail->next=front;
delete s;
}
cur_size--;
return true;
}
bool push_back(const T& t)
{
Node<T> *s=new Node<T>(t);
return push_back(s);
}
bool insert(size_t pos,Node<T>* n)throw (ArgIsIll)
{
if(pos>this->size())
{
throw ArgIsIll();
}
else if(pos==this->size())
{
return push_back(n);
}
else if(pos==0)
{
return push_front(n);
}
else
{
size_t i=0;
Node<T>* cur=front;
for(i=0;i<pos-1;++i)
{
cur=cur->next;
}
n->next=cur->next;
cur->next->prev=n;
n->prev=cur;
cur->next=n;
cur_size++;
return true;
}
}
bool insert(size_t pos,const T& t)throw (ArgIsIll)
{
Node<T>* s=new Node<T>(t);
return insert(pos,s);
}
bool erase(size_t pos)
{
if(pos>this->size())
{
throw ArgIsIll();
}
else if(pos==this->size())
{
return pop_back();
}
else if(pos==0)
{
return pop_front();
}
else
{
size_t i=0;
Node<T>* cur=front;
for(i=0;i<pos-1;++i)
{
cur=cur->next;
}
cur->next->prev=cur->prev;
cur->prev->next=cur->next;
delete cur;
cur_size--;
return true;
}
}
void print()const
{
Node<T>* cur=front;
while(true)
{
cout<<cur->data<<" ";
if(cur==tail)
break;
cur=cur->next;
}
cout<<endl;
}
private:
List(const List& l);
private:
Node<T>* front;
Node<T>* tail;
size_t cur_size;
};
#endif

以上

如果你有任何想法或是可以改进的地方,欢迎和我交流!

完整代码及测试用例在github上:点我前往