C++ STL之list的使用及模拟实现
#pragma once
#include<iostream>
using namespace std;
namespace my_list
{
// List的节点类
template<class T>
struct ListNode
{
ListNode(const T& val = T())
:_pPre(nullptr)
,_pNext(nullptr)
,_val(val)
{}
ListNode<T>* _pPre;
ListNode<T>* _pNext;
T _val;
};
//List的迭代器类
template<class T, class Ref, class Ptr>
class List_iterator
{
typedef ListNode<T>* PNode;
typedef List_iterator<T, Ref, Ptr> Self;
public:
List_iterator(PNode pNode = nullptr)
{
_pNode = pNode;
}
List_iterator(const Self& l)
{
_pNode = l._pNode;
}
Ref operator*()
{
return _pNode->_val;
}
Ptr 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 _pNode != l._pNode;
}
bool operator==(const Self& l)
{
return _pNode == l._pNode;
}
PNode GetNode()
{
return _pNode;
}
private:
PNode _pNode;
};
// 反向迭代器——对正向迭代器的接口进行包装
template<class Iterator, class Ref, class Ptr>
struct Reverse_iterator
{
Iterator _it;
typedef Reverse_iterator<Iterator, Ref, Ptr> Self;
Reverse_iterator() {}
Reverse_iterator(Iterator it)
: _it(it)
{}
Ref operator*()
{
Iterator tmp(_it);
--tmp;
return *tmp;
}
Ptr operator->()
{
return &(operator*());
}
Self& operator++()
{
--_it;
return *this;
}
Self operator++(int)
{
Self tmp(*this);
--_it;
return tmp;
}
Self& operator--() {
++_it;
return *this;
}
Self operator--(int)
{
Self tmp(*this);
++_it;
return tmp;
}
bool operator!=(const Self& s)
{
return _it != s._it;
}
bool operator==(const Self& s)
{
return _it == s._it;
}
};
//list类
template<class T>
class list
{
typedef ListNode<T> Node;
typedef Node* PNode;
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:
///
// List Iterator
iterator begin()
{
return _pHead->_pNext;
}
iterator end()
{
return _pHead;
}
const_iterator begin() const
{
return const_iterator(_pHead->_pNext);
}
const_iterator end() const
{
return const_iterator(_pHead);
}
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());
}
///
// List 构造/赋值
list()
{
CreateHead();
}
list(int n, const T& value = T())
{
CreateHead();
while (n--)
{
push_back(value);
}
_size = n;
}
list(int n, T& value = T())
{
CreateHead();
while (n--)
{
push_back(value);
}
_size = n;
}
template <class Iterator>
list(Iterator first, Iterator last)
{
CreateHead();
while (first != last)
{
push_back(*first);
first++;
_size++;
}
}
list(const list<T>& l)
{
CreateHead();
for (auto it : l)
{
push_back(it);
_size++;
}
}
void swap(list<T>& l)
{
std::swap(this->_pHead, l._pHead);
std::swap(this->_size, l._size);
}
list<T>& operator=(list<T> l)
{
swap(l);
return *this;
}
~list()
{
clear();
delete _pHead;
_pHead = nullptr;
}
///
// List Capacity
size_t size()const
{
return _size;
}
bool empty()const
{
return _size == 0;
}
// List Access
T& front()
{
assert(!empty());
return _pHead->_pNext->_val;
}
const T& front()const
{
assert(!empty());
return _pHead->_pNext->_val;
}
T& back()
{
assert(!empty());
return _pHead->_pPre->_val;
}
const T& back()const
{
assert(!empty());
return _pHead->_pPre->_val;
}
// List Modify
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());
}
// 在pos位置前插入值为val的节点
iterator insert(iterator pos, const T& val)
{
PNode tmp = new Node(val);
PNode cur = pos.GetNode();
PNode pre = cur->_pPre;
tmp->_pNext = cur;
tmp->_pPre = pre;
pre->_pNext = tmp;
cur->_pPre = tmp;
_size++;
return tmp;
}
// 删除pos位置的节点,返回该节点的下一个位置
iterator erase(iterator pos)
{
PNode cur = pos.GetNode();
PNode next = cur->_pNext;
PNode pre = cur->_pPre;
delete cur;
pre->_pNext = next;
next->_pPre = pre;
_size--;
return next;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
private:
// 创建头结点
void CreateHead()
{
_pHead = new Node;
_pHead->_pNext = _pHead->_pPre = _pHead;
}
PNode _pHead;
size_t _size = 0;
};
};