话不多说上代码:
#pragma once
#include<iostream>
#include<assert.h>
#include"_reverse_iterator.h"
using namespace std;
namespace my_list
{
template<class T>
struct list_node
{
list_node(const T&val=T())
:_data(val)
,_next(nullptr)
,_prev(nullptr)
{
}
T _data;
list_node<T>* _next;
list_node<T>* _prev;
};
template<class T,class Ref,class Ptr>
struct __list_iterator
{
typedef list_node<T> Node;
typedef __list_iterator<T, Ref, Ptr> iterator;
//typedef __list_iterator<T, Ref, Ptr> const_iterator;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{
}
bool operator!=(const iterator& it)const
{
return it._node != _node;
}
bool operator==(const iterator& it)const
{
return it._node == _node;
}
//++it
//iterator& operator++()
iterator& operator++()
{
_node =_node->_next;
return *this;
}
//--it
iterator& operator--()
{
_node = _node->_prev;
return *this;
}
//it++
iterator operator++(int)
{
iterator tmp(this);
_node = _node->_next;
return tmp;
}
//it--
iterator operator--(int)
{
iterator tmp(this);
_node = _node->_prev;
return tmp;
}
//T& operator*()
Ref operator*()
{
return _node->_data;
}
//T* operator->()const
Ptr operator->()const
{
return &(operator*());
}
};
template<class T>
class list
{
public:
typedef list_node<T> Node;
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T,const T&, const T*> const_iterator;
typedef _reverse_iterator<iterator, T&, T*> RIterator;
typedef _reverse_iterator<const_iterator, const T&, const T*> const_RIterator;
list()
{
empty_init();
}
template<class InputIterator>
list(InputIterator first, InputIterator last)
{
empty_init();
while(first != last)
{
push_back(*first);
++first;
}
}
list(const list<T><)
{
empty_init();
list<T>tmp(lt.begin(),lt.end());
swap(tmp);
}
void swap(list<T>& lt)
{
std::swap(_head, lt._head);
}
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
void push_back(const T& val)
{
/*Node* newnode = new Node(val);
Node* tail = _head->_prev;
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;*/
insert(_head, val);
}
void push_front(const T& val)
{
insert(begin(), val);
}
iterator insert(iterator pos,const T&val)
{
Node* newnode = new Node(val);
Node* prev = pos._node->_prev;
newnode->_prev = prev;
prev->_next = newnode;
newnode->_next = pos._node;
pos._node->_prev = newnode;
return iterator(newnode);
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* next = pos._node->_next;
pos._node->_prev->_next = pos._node->_next;
pos._node->_next->_prev = pos._node->_prev;
delete pos._node;
return iterator(next);
}
list<T>& operator=( list<T> lt)
{
swap(lt);
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
void clear()
{
while (this->begin() != this->end())
{
erase(this->begin());
}
}
void pop_back()
{
erase(_head->_prev);
}
void pop_front()
{
erase(begin());
}
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
const_iterator begin()const
{
return const_iterator(_head->_next);
}
const_iterator end()const
{
return const_iterator(_head);
}
RIterator rbegin()
{
return RIterator(end());
}
RIterator rend()
{
return RIterator(begin());
}
private:
Node* _head;
};
void test_mylist()
{
list<int> L1;
L1.push_back(1);
L1.push_back(2);
L1.push_back(3);
L1.push_back(4);
L1.push_back(5);
auto it = L1.rbegin();
list<int>copy(L1);
while (it != L1.rend())
{
*it *= 2;
cout << *it << endl;
++it;
}
//L1.pop_back();
it = L1.begin();
//L1.clear();
//for (auto e : copy)
//{
// cout << e << endl;
//}
}
}
反向迭代器:
#pragma once
namespace my_list
{
template<class Iterator,class Ref,class Ptr>
struct _reverse_iterator
{
typedef _reverse_iterator<Iterator, Ref, Ptr> RIterator;
Iterator _cur;
_reverse_iterator(Iterator It)
:_cur(It)
{
}
RIterator operator++()
{
--_cur;
return *this;
}
RIterator operator--()
{
++_cur;
return *this;
}
Ref operator*()
{
auto tmp(_cur);
return *(--tmp);
}
bool operator!=(const RIterator & It)
{
return _cur!=It._cur;
}
};
}