template <class T>
struct list_node{ //[1]
T _data; //[2]
list_node *_next; //指向下一个节点
list_node *_prev; //指向前一个节点
list_node(const T &val = T())
:_data(val),
_next(nullptr),
_prev(nullptr)
{}
};
template <class T>
class Mylist{
typedef list_node<T> Node; //[3]
Node *_phead; //[4]
//......
};
解释
//list_iterator
template <class T, class Ref, class Ptr>
struct list_iterator{ //[1]
typedef list_node<T> Node;
typedef list_iterator<T, Ref, Ptr> iterator;
Node *_pnode; //[2]
list_iterator(Node *pnode) //[3]
:_pnode(pnode)
{}
解释:
//begin() & end()
template <class T>
class Mylist{
//......
public:
//iterator
typedef list_iterator<T, T&, T*> iterator;
typedef list_iterator<T, const T&, const T*> const_iterator; //[2]
iterator begin(){
return iterator(_phead->_next); //[1]
}
iterator end(){
return iterator(_phead);
}
const_iterator begin() const{ //[3]
return const_iterator(_phead->_next);
}
const_iterator end() const{
return const_iterator(_phead);
//......
}
解释:
Ref operator*() const{ //[3]
return _pnode->_data; //[1]
}
Ptr operator->() const{ //[3]
return &(_pnode->_data); //[2]
}
//下面的代码使用->访问对象的成员:
struct Pos{
int _row;
int _col;
Pos(int row = 0, int col = 0)
:_row(row),
_col(col)
{}
};
void Test7(){
cout << "Test7: " << endl;
Mylist<Pos> mlt; //链表存储的数据类型是自定义类型
mlt.push_back(Pos(10,10));
mlt.push_back(Pos(20,20));
for(auto it = mlt.begin(); it != mlt.end(); ++it)
{
cout << it->_row << ":" << it->_col << endl; //[4]
}
}
解释:
it->->_row
:
bool operator!=(const iterator &it) const{ //[1]
return _pnode!=it._pnode;
}
bool operator==(const iterator &it) const{ //[1]
return _pnode==it._pnode;
}
解释:
iterator& operator++(){
_pnode = _pnode->_next; //[1]
return *this;
}
iterator operator++(int){
iterator tmp(*this); //[2]
_pnode = _pnode->_next;
return tmp;
}
解释:
提示:还有前置--和后置--,只需要访问节点的_prev将指针指向前一个节点即可。
void empty_initialize(){ //[1]
_phead = new Node;
_phead->_next = _phead;
_phead->_prev = _phead;
}
Mylist(){
empty_initialize();
}
template <typename InputIterator>
Mylist(InputIterator first, InputIterator last){
empty_initialize(); //[2]
while(first!=last)
{
push_back(*first);
++first;
}
}
解释:
//void swap(Mylist &mlt)
void swap(Mylist &mlt){ //[1]
std::swap(_phead, mlt._phead);
}
//Mylist(const Mylist &mlt)
Mylist(const Mylist &mlt){ //[1]
empty_initialize(); //交换前必须要进行空初始化,否则会访问野指针崩溃
Mylist tmp(mlt.begin(), mlt.end());
swap(tmp);
}
Mylist& operator=(Mylist mlt){
swap(mlt);
return *this;
}
解释:
void clear(){
iterator it = begin();
while(it != end())
{
it = erase(it);
}
}
~Mylist(){
clear(); //清除所有数据节点
delete _phead; //释放哨兵位节点
}
注意:一定记得释放哨兵位节点哦!
iterator insert(iterator pos, const T &val){
Node *cur = pos._pnode; //[1]
Node *prev = cur->_prev;
Node *newnode = new Node(val);
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
return iterator(newnode); //[2]
}
解释:
void push_back(const T &val){
insert(end(), val);
}
void push_front(const T &val){
insert(begin(), val);
}
iterator erase(iterator pos){
assert(pos != end()); //[1]
Node *cur = pos._pnode;
Node *prev = cur->_prev;
Node *next = cur->_next;
next->_prev = prev;
prev->_next = next;
delete cur;
return iterator(next); //[2]
}
解释:
void pop_back(){
assert(_phead->_next != _phead); //[1]
erase(--end());
}
void pop_front(){
assert(_phead->_next != _phead);
erase(begin());
}
注意:pop之前要判空!