模拟实现
namespace mine
{
template<class T>
struct ListNode
{
ListNode<T>* _pPre;
ListNode<T>* _pNext;
T _val;
ListNode(const T& val = T())
:_pPre(nullptr)
, _pNext(nullptr)
, _val(val)
{}
};
template<class T, class Ref, class Ptr>
class ListIterator
{
typedef ListNode<T>* PNode;
typedef ListIterator<T, Ref, Ptr> Self;
public:
ListIterator(PNode pNode)
:_pNode(pNode)
{}
ListIterator(const Self& l)
:_pNode(l._pNode)
{}
T& operator*()
{
return _pNode->_val;
}
T* operator->()
{
return &_pNode->_val;
}
Self& operator++()
{
_pNode = _pNode->_pNext;
return *this;
}
Self operator++(int)
{
Self tmp(*this);
_pNode = _pNode->_pNext;
return tmp;
}
Self& operator--()
{
_pNode = _pNode->_pPre;
return *this;
}
Self& operator--(int)
{
Self tmp(*this);
_pNode = _pNode->_pPre;
return tmp;
}
bool operator!=(const Self& l)
{
return this->_pNode != l._pNode;
}
bool operator==(const Self& l)
{
return this->_pNode == l._pNode;
}
PNode _pNode;
};
template<class T>
class list
{
typedef ListNode<T> Node;
typedef Node* PNode;
public:
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T&> const_iterator;
public:
list()
{
CreateHead();
}
list(int n, const T& value = T())
{
CreateHead();
for (int i = 0; i < n; i++)
{
push_back(value);
}
}
template <class Iterator>
list(Iterator first, Iterator last)
{
CreateHead();
while (first != last)
{
push_back(*first);
first++;
}
}
list(const list<T>& l)
{
this->CreateHead();
for (auto& e : l)
{
this->push_back(e);
}
}
list<T>& operator=(const list<T> l)
{
this->swap(l);
return *this;
}
~list()
{
this->clear();
delete _pHead;
_pHead = nullptr;
}
iterator begin()
{
return this->_pHead->_pNext;
}
iterator end()
{
return this->_pHead;
}
const_iterator begin() const
{
return this->_pHead->_pNext;
}
const_iterator end() const
{
return this->_pHead;
}
size_t size()const
{
return _size;
}
bool empty()const
{
return _size == 0;
}
T& front()
{
return this->_pHead->_pPre->_val;
}
const T& front()const
{
return _pHead->_pPre->_val;
}
T& back()
{
return this->_pHead->_pNext->_val;
}
const T& back()const
{
return this->_pHead->_pNext->_val;
}
void push_back(const T& val) { insert(end(), val); }
void pop_back() { erase(--end()); }
void push_front(const T& val) { insert(begin(), val); }
void pop_front() { erase(begin()); }
void insert(iterator pos, const T& val)
{
PNode cur = pos._pNode;
PNode pre = pos._pNode->_pPre;
PNode newnode = new Node(val);
pre->_pNext = newnode;
newnode->_pPre = pre;
newnode->_pNext = cur;
cur->_pPre = newnode;
_size++;
}
iterator erase(iterator pos)
{
PNode cur = pos._pNode;
PNode pre = cur->_pPre;
PNode next = cur->_pNext;
delete cur;
pre->_pNext = next;
next->_pPre = pre;
_size--;
return next;
}
void clear()
{
auto it = this->begin();
while (it != this->end())
{
it=erase(it);
}
}
void swap(list<T>& l)
{
std::swap(_pHead, l._pHead);
std::swap(_size, l._size);
}
private:
void CreateHead()
{
_pHead = new Node;
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
_size = 0;
}
PNode _pHead;
size_t _size;
};
};