头文件
#pragma once
#ifndef LIST_H
#define LIST_H
#include<initializer_list>
namespace liujin
{
template<class T>
void Swap(T& x, T& y)
{
T tmp = x;
x = y;
y = tmp;
}
template<class _Ty>
class list
{
private:
struct _Node;
typedef struct _Node* _Nodeptr;
struct _Node
{
_Nodeptr _Prev, _Next;
_Ty _value;
};
struct _Acc;
struct _Acc
{
typedef _Node*& _Nodepref;
typedef _Ty& _Vref;
static _Vref Get_Value(_Nodepref p)
{
return p->_value;
}
static _Nodepref Get_Prev(_Nodepref p)
{
return p->_Prev;
}
static _Nodepref Get_Next(_Nodepref p)
{
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 iterator;
class const_iterator;
class const_iterator
{
protected:
_Nodeptr _Ptr;
public:
const_iterator(_Nodeptr _P) :_Ptr(_P) {}
const_reference operator*()
{
return _Acc::Get_Value(_Ptr);
}
const_pointer operator->()
{
return &**this;
}
const_iterator& operator++()
{
_Ptr = _Acc::Get_Next(_Ptr);
return *this;
}
const_iterator operator++(int)
{
const_iterator tmp = *this;
_Ptr = _Acc::Get_Next(_Ptr);
return tmp;
}
const_iterator& operator--()
{
_Ptr = _Acc::Get_Prev(_Ptr);
return *this;
}
const_iterator operator--(int)
{
const_iterator tmp = *this;
_Ptr = _Acc::Get_Prev(_Ptr);
return tmp;
}
bool operator==(const const_iterator& it)
{
return this->_Ptr == it._Ptr;
}
bool operator!=(const const_iterator& it)
{
return !(this->_Ptr == it._Ptr);
}
_Nodeptr _Mynode()const
{
return _Ptr;
}
};
class iterator:public const_iterator
{
public:
iterator(_Nodeptr _p) :const_iterator(_p) {}
reference operator*()
{
return _Acc::Get_Value(this->_Ptr);
}
pointer operator->()const
{
return &_Acc::Get_Value(this->_Ptr);
}
iterator& operator++()
{
this->_Ptr = _Acc::Get_Next(this->_Ptr);
return *this;
}
iterator operator++(int)
{
iterator tmp = *this;
this->_Ptr = _Acc::Get_Next(this->_Ptr);
return tmp;
}
iterator& operator--()
{
this->_Ptr = _Acc::Get_Prev();
return *this;
}
iterator operator--(int)
{
iterator tmp = *this;
this->_Ptr = _Acc::Get_Prev();
return tmp;
}
bool operator==(const iterator& it)const
{
return it._Ptr == this->_Ptr;
}
bool operator!=(const iterator& it)const
{
return !(it == *this);
}
};
public:
iterator begin() {
return iterator(_Acc::Get_Next(_Head));
}
iterator end() {
return iterator(_Head);
}
const_iterator begin()const {
return const_iterator(_Acc::Get_Next(_Head));
}
const_iterator end()const {
return const_iterator(_Head);
}
public:
typedef const_iterator _It;
list():_Head(_Buynode()),_Size(0){}
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(std::initializer_list<_Ty> list):list()
{
for (auto& X : list)
{
push_back(X);
}
}
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;
}
erase(_F1,_L1);
insert(_L1, _F2, _L2);
return*this;
}
~list()
{
clear();
_Freenode(_Head);
}
iterator insert(iterator _P, const _Ty& val)
{
_Nodeptr _S = _P._Mynode ();
_Acc::Get_Prev(_S) = _Buynode(_Acc::Get_Prev(_S), _S);
_S = _Acc::Get_Prev(_S);
_Acc::Get_Next(_Acc::Get_Prev(_S)) = _S;
new(&_Acc::Get_Value(_S)) _Ty(val);
_Size++;
return iterator(_S);
}
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);
}
}
void push_front(const _Ty&val)
{
insert(begin(), val);
}
void push_back(const _Ty& val)
{
insert(end(), val);
}
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 _F = begin(), _L = end();
while (_F!= _L)
{
if (*_F== val)
{
erase(_F);
return;
}
_F++;
}
}
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();
_Acc::Get_Next(_Acc::Get_Prev(_S)) = _Acc::Get_Next(_S);
_Acc::Get_Prev(_Acc::Get_Next(_S)) = _Acc::Get_Prev(_S);
(&_Acc::Get_Value(_S))->~_Ty();
_Freenode(_S);
_Size -= 1;
return _P;
}
void Swap(list& _X)
{
liujin::Swap(_X._Head(), _Head);
liujin::Swap(_X._Size, _Size);
}
private:
_Nodeptr _Buynode(_Nodeptr _Parg = NULL, _Nodeptr _Narg = NULL)
{
_Nodeptr _S = (_Nodeptr)malloc(sizeof(_Node));
_Acc::Get_Next(_S)=_Narg == NULL ? _S : _Narg;
_Acc::Get_Prev(_S)=_Parg == NULL ? _S : _Parg;
return _S;
}
void _Freenode(_Nodeptr _p)
{
free(_p);
}
_Nodeptr _Head;
size_type _Size;
};
}
测试函数:
#if 1
#include<iostream>
using namespace std;
#include"List.h"
int main()
{
#if 0
liujin::list<int> ilist, ilist1;
liujin::list<int>::iterator it = ilist.begin();
liujin::list<int>::iterator it1=ilist1.begin();
ilist.insert(it, 12);
ilist.push_back(23);
ilist.push_front(0);
int ar[] = { 12,23,34,45,56,67,78,89,90 };
int len = sizeof(ar) / sizeof(ar[0]);
ilist.insert(it, ar, ar + len);
ilist.insert(it, 10, 23);
ilist1.insert(ilist1.begin(), ilist.begin(), ilist.end());
for (it = ilist.begin(); it != ilist.end(); it++)
{
cout << *it << " ";
}
cout << endl;
for (it1 = ilist1.begin(); it1 != ilist1.end(); it1++)
{
cout << *it1 << " ";
}
{
liujin::list<int> ailist;
ilist.Swap(ailist);
}
return 0;
#endif
#if 0
liujin::list<int> ilistb = { 1,2,34,5,6,7,8 };
ilistb.insert(ilistb.begin(), { 1,2,3,4,5,6 });
#endif
}
#endif
#include<list>
#if 0
int main()
{
int ar[10] = { 1,2,3,4,56,67,7,8,9,10 };
int len = sizeof(ar) / sizeof(ar[0]);
list<int> ar1;
list<int>ar2(ar,ar+len);
list<int>ar3(10, 23);
ar3.insert(ar3.begin(), 24);
for (auto it = ar3.begin(); it != ar3.end(); it++)
{
cout << *it << " ";
}
}
#endif
#if 0
#include<initializer_list>
std::initializer_list<int>fun()
{
int a = 1, b = 2, c = 3;
return { a,b,c };
}
class object
{
int val;
public:
object(int x=0):val(x){}
};
int main()
{
std::initializer_list<int>ar1 = fun();
std::initializer_list<int> ar = { 12,23,34,45,56,67,78 };
int n = ar.size();
for (auto it = ar.begin(); it != ar.end(); it++)
{
cout << *it << " ";
}
cout << endl;
object a(10), b(20), c(30);
std::initializer_list<object> ar3 = { a,b,c };
}
#endif
#if 0
typedef int* Pint;
using Pint = int*;
#endif
|