cpp
复制代码
#pragma once
#include <cassert>
namespace zs
{
template<class T>
class list_node
{
public:
list_node(const T& val = T())
: _data(val)
, _next(nullptr)
, _prev(nullptr)
{}
public:
T _data;
list_node<T>* _next;
list_node<T>* _prev;
};
// 反向迭代器
template<class Iterator, class Ref, class Ptr>
class __reverse_iterator
{
public:
typedef __reverse_iterator<Iterator, Ref, Ptr> reverse_iterator;
public:
__reverse_iterator(Iterator it)
:_cur(it)
{}
reverse_iterator& operator++()
{
--_cur;
return *this;
}
reverse_iterator& operator--()
{
++_cur;
return *this;
}
reverse_iterator operator++(int)
{
reverse_iterator tmp = *this;
--_cur;
return tmp;
}
reverse_iterator operator--(int)
{
reverse_iterator tmp = *this;
++_cur;
return tmp;
}
Ref operator*()
{
Iterator tmp = _cur;
--tmp;
return *tmp;
}
Ptr operator->()
{
return &(operator*());
}
bool operator!=(const reverse_iterator& it)
{
return _cur != it._cur;
}
bool operator==(const reverse_iterator& it)
{
return _cur == it._cur;
}
public:
Iterator _cur;
};
// 正向迭代器
template<class T, class Ref, class Ptr>
class __list_iterator
{
private:
typedef list_node<T> Node;
typedef __list_iterator<T, Ref, Ptr> iterator;
public:
__list_iterator(Node* node)
: _node(node)
{}
bool operator!=(const iterator& it) const
{
return _node != it._node;
}
bool operator==(const iterator& it) const
{
return _node == it._node;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(operator*());
}
iterator& operator++()
{
_node = _node->_next;
return *this;
}
iterator operator++(int)
{
iterator ret(*this);
_node = _node->_next;
return ret;
}
iterator& operator--()
{
_node = _node->_prev;
return *this;
}
iterator operator--(int)
{
iterator ret(*this);
_node = _node->_prev;
return ret;
}
public:
Node* _node;
};
template<class T>
class list
{
private:
typedef list_node<T> Node;
public:
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
typedef __reverse_iterator<iterator, T&, T*> reverse_iterator;
typedef __reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
public:
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
const_iterator begin() const
{
return const_iterator(_head->_next);
}
const_iterator end() const
{
return const_iterator(_head);
}
reverse_iterator rbegin()
{
return reverse_iterator(end());
}
reverse_iterator rend()
{
return reverse_iterator(begin());
}
const_reverse_iterator rbegin() const
{
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const
{
return const_reverse_iterator(begin());
}
public:
list()
{
empty_init();
}
template <class InputIterator>
list(InputIterator first, InputIterator last)
{
empty_init();
while (first != last)
{
push_back(*first);
++first;
}
}
list(const list<T>& lt)
{
empty_init();
list<T> tmpList = list(lt.begin(), lt.end());
swap(tmpList);
}
list(int n, const T& val = T())
{
empty_init();
int i = 0;
while(i++ < n)
{
push_back(val);
}
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
list<T>& operator=(list<T> lt)
{
swap(lt);
return *this;
}
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
void swap(list<T>& lt)
{
::swap(_head, lt._head);
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
void push_front(const T& val)
{
insert(begin(), val);
}
void push_back(const T& val)
{
insert(end(), val);
}
void pop_front()
{
erase(begin());
}
void pop_back()
{
erase(-end());
}
iterator insert(iterator pos, const T& val)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(val);
prev->_next = newnode;
newnode->_prev = prev;
cur->_prev = newnode;
newnode->_next = cur;
return iterator(newnode);
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
return iterator(next);
}
T& front()
{
return *(begin());
}
T& back()
{
return *(--end());
}
const T& front() const
{
return *(begin());
}
const T& back() const
{
return *(--end());
}
bool empty() const
{
return _head->_next == _head;
}
size_t size() const
{
size_t count = 0;
list<T>::const_iterator it = begin();
while (it != end())
{
++it;
++count;
}
return count;
}
private:
Node* _head;
};
}