namespace bing
{
// 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 ListIterator
{
typedef ListNode<T>* PNode;
typedef ListIterator<T, Ref, Ptr> Self;
public:
ListIterator(PNode pNode = nullptr)
:_pNode(pNode)
{}
ListIterator(const Self& l)
:_pNode(l._pNode)
{}
T& operator*() {
return _pNode->_val;
}
T* operator->() {
return &(operator*());
}
Self& operator++() {
_pNode = _pNode->_pNext;
return *this;
}
Self& operator++(int) {
Self temp(*this);
_pNode = _pNode->_pNext;
return temp;
}
Self& operator--() {
_pNode = _pNode->_pPre;
return *this;
}
Self& operator--(int) {
Self temp = _pNode->_pPre;
_pNode = _pNode->_pPre;
return temp;
}
bool operator!=(const Self& l) {
return _pNode != l._pNode;
}
bool operator==(const Self& l) {
return _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;
///
// List Iterator
public:
iterator begin() {
return iterator(_pHead->_pNext);
}
iterator end() {
return iterator(_pHead);
}
const_iterator cbegin() const{
return const_iterator(_pHead->_pNext);
}
const_iterator cend() const{
return const_iterator(_pHead);
}
///
// List的构造
list() {
CreateHead();
}
list(int n, const T& value = T()) {
CreateHead();
while (n) {
push_back(value);
n--;
}
}
template <class Iterator>
list(Iterator first, Iterator last) {
CreateHead();
while (first != last) {
push_back(*first);
first++;
}
}
list(const list<T>& l) {
CreateHead();
list<int> tmp(l.cbegin(), l.cend());
this->swap(tmp);
}
list<T>& operator=(list<T> l) {
swap(l);
return *this;
}
~list() {
clear();
delete[] _pHead;
_pHead = nullptr;
}
// 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 pnn = new Node(val);
PNode pcur = pos._pNode;
pnn->_pNext = pcur;
pnn->_pPre = pcur->_pPre;
pcur->_pPre->_pNext = pnn;
pcur->_pPre = pnn;
return pnn;
}
// 删除pos位置的节点,返回该节点的下一个位置
iterator erase(iterator pos) {
PNode pdel = pos._pNode;
PNode pret = pdel->_pNext;
pdel->_pPre->_pNext = pret;
pret->_pPre = pdel->_pPre;
delete pdel;
return iterator(pret);
}
void clear() {
iterator p = begin();
while (p != end()) {
p = erase(p);
}
}
void swap(list<T>& l) {
PNode tmp = _pHead;
_pHead = l._pHead;
l._pHead = tmp;
}
///
// List Capacity
size_t size()const {
size_t sz = 0;
ListNode<T>* p = _pHead->_pNext;
while (p != _pHead) {
sz++;
p = p->_pNext;
}
return sz;
}
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;
}
private:
void CreateHead() {
_pHead = new Node;
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
}
private:
PNode _pHead;
};
}
list类的实现
最新推荐文章于 2024-07-25 19:26:22 发布