#ifndef ZJH_LIST_H
#define ZJH_LISH_H
#include<iostream>
using namespace std;
namespace yhp
{
template<class T>
void Swap(T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
template<class _Ty>
class list
{
protected:
struct _Node;
typedef struct _Node* _Nodeptr;
struct _Node
{
_Nodeptr _Prev, _Next;
_Ty _Value;
};
struct _Acc;
struct _Acc
{
typedef struct _Node*& _Nodepref;
typedef _Ty& _Vref;
static _Vref _Value(_Nodeptr _P)
{
return (*_P)._Value;
}
static _Nodepref _Prev(_Nodeptr _P)
{
return (*_P)._Prev;
}
static _Nodepref _Next(_Nodeptr _P)
{
return (*_P)._Next;//return _P->Next;
}
};
public:
typedef _Ty value_type;
typedef _Ty& reference;
typedef const _Ty& const_reference;
typedef _Ty* pointer;
typedef const _Ty* const_pointer;
typedef size_t size_type;
typedef int difference_type;
public:
class const_iterator
{
protected:
_Nodeptr _Ptr;
public:
const_iterator(_Nodeptr _P = NULL) :_Ptr(_P) {}
const_reference operator*()const
{
return _Acc::_Value(_Ptr);
}
const_pointer operator->()const
{
return &**this;
}
const_iterator& operator++()
{
_Ptr = _Acc::_Next(_Ptr);
return *this;
}
const_iterator operator++(int)
{
const_iterator tmp = *this;
++* this;
return tmp;
}
const_iterator operator--()
{
_Ptr = _Acc::_Prev(_Ptr);
return *this;
}
const_iterator operator--(int)
{
const_iterator tmp = *this;
--* this;
return tmp;
}
bool operator==(const const_iterator& _X)const
{
return this->_Ptr == _X._Ptr;
}
bool operator!=(const const_iterator& _X)const
{
return !(*this == _X);
}
_Nodeptr _Mynode()const
{
return _Ptr;
}
};
class iterator :public const_iterator
{
typedef const_iterator base;
public:
iterator(_Nodeptr _P = NULL) :const_iterator(_P) {}
iterator& operator++()
{
base::_Ptr = _Acc::_Next(base::_Ptr);
return *this;
}
iterator operator++(int)
{
iterator tmp = *this;
++* this;
return tmp;
}
iterator& operator--()
{
base::_Ptr = _Acc::_Prev(base::_Ptr);
return *this;
}
iterator operator--(int)
{
iterator tmp = *this;
--* this;
return tmp;
}
bool operator==(const iterator& _X)const
{
return this->_Ptr == _X._Ptr;
}
bool operator!=(const iterator& _X)const
{
return !(*this == _X);
}
};
public:
iterator begin()
{
return iterator(_Acc::_Next(_Head));
}
iterator end()
{
return iterator(_Head);
}
const_iterator begin()const
{
return const_iterator(_Acc::_Next(_Head));
}
const_iterator end()const
{
return const_iterator(_Head);
}
public:
typedef const_iterator _It;
list() :_Head(_Buynode()), _Size(0) {}
list(std::initializer_list<_Ty>list) :list()
{
for (auto& x : list)
{
push_back(x);
}
}
list(size_t count, const _Ty& val) :_Head(_Buynode()), _Size(0)
{
insert(begin(), count, val);
}
list(const _Ty* _F, const _Ty* _L) :_Head(_Buynode()), _Size(0)
{
insert(begin(), _F, _L);
}
list(const list& _X) :_Head(_Buynode()), _Size(0)
{
insert(begin(), _X.begin(), _X.end());
}
list& operator=(const list _X)
{
if (this == &_X)return *this;
iterator _F1 = begin();
iterator _L1 = end();
const_iterator _F2 = _X.begin();
const_iterator _L2 = _X.end();
for (; _F1 != _L1 && _F2 != _L2; ++_F1, ++_F2)
{
*_F1 = *_F2;
}
earse(_F1, _L1);
insert(_L1, _F2, _L2);
return *this;
}
~list()
{
clear();
_Freenode(_Head);
}
void push_front(const _Ty& val)
{
insert(begin(), val);
}
void push_back(const _Ty& val)
{
insert(end(), val);
}
void insert(iterator _P, const _Ty* _F, const _Ty* _L)
{
for (; _F != _L; ++_F)
{
insert(_P, *_F);
}
}
void insert(iterator _P, size_t count, const _Ty& val)
{
while (count--)
{
insert(_P, val);
}
}
void insert(iterator _P, _It _F, _It _L)
{
for (; _F != _L; ++_F)
{
insert(_P, *_F);
}
}
void insert(iterator _P, std::initializer_list<_Ty>list)
{
for (auto& x : list)
{
insert(_P, x);
}
}
iterator insert(iterator _P, const _Ty& val)
{
_Nodeptr _S = _P._Mynode();
_Acc::_Prev(_S) = _Buynode(_Acc::_Prev(_S), _S);
_S = _Acc::_Prev(_S);
_Acc::_Next(_Acc::_Prev(_S)) = _S;
new(&_Acc::_Value(_S))_Ty(val);
_Size += 1;
return iterator(_S);
}
void pop_front()
{
erase(begin());
}
void pop_back()
{
erase(--end());
}
void erase(iterator _F, iterator _L)
{
for (; _F != _L;)
{
erase(_F++);
}
}
void clear()
{
erase(begin(), end());
}
void remove(const _Ty& val)
{
iterator _P = begin(), _L = end();
for (; _P != end(); _P++)
{
if (*_P == val)
{
erase(_P);
break;
}
}
}
void remove_all(const _Ty& val)
{
iterator _F = begin(), _L = end();
while (_F != _L)
{
if (*_F == val)
{
erase(_F++);
}
else
{
++_F;
}
}
}
iterator erase(iterator _P)
{
_Nodeptr _S = _P._Mynode();
//_Nodeptr _S=_P.operator++(0)._Mynode();
// 这里是运算符重载的函数,不是单纯的++
//_tmp 其实是临时量_tmp调动的_Mynode
//_P
_Acc::_Prev(_Acc::_Next(_S)) = _Acc::_Prev(_S);
_Acc::_Next(_Acc::_Prev(_S)) = _Acc::_Next(_S);
(&_Acc::_Value(_S))->~_Ty();
_Freenode(_S);
_Size -= 1;
return _P;
//此时返回的_P其实是一个空指针,为了解决这个问题
}
//void Swap(list _P)
//{
// //空间收缩
// yhp::Swap(this->)
//}
private:
_Nodeptr _Buynode(_Nodeptr _Parg = NULL, _Nodeptr _Narg = NULL)
{
_Nodeptr _S = (_Nodeptr)malloc(sizeof(_Node));
_Acc::_Prev(_S) = _Parg == NULL ? _S : _Parg;
_Acc::_Prev(_S) = _Narg == NULL ? _S : _Narg;
return _S;
}
void _Freenode(_Nodeptr _P)
{
free(_P);
}
_Nodeptr _Head;
size_type _Size;
};
}
//自定义类型的运算符是按照函数规则来进行的,和内置类型不一样
STL链表
于 2022-03-21 20:27:33 首次发布