STL - list
- [STL - list](#STL - list)
-
-
- [结点 ListNode](#结点 ListNode)
- [迭代器 ListIterator](#迭代器 ListIterator)
- [反向迭代器 Reverse_iterator](#反向迭代器 Reverse_iterator)
- [管理操作 list](#管理操作 list)
-
STL - list
结点 ListNode
作为储存数据的单元位。
cpp
template<class T>
struct ListNode
{
T _val;
ListNode* _next;
ListNode* _prev;
ListNode()
:_val(0)
,_next(nullptr)
,_prev(nullptr)
{}
};
迭代器 ListIterator
作为访问节点的工具。
cpp
template<class T,class Ptr, class Ref>
class ListIterator
{
private:
typedef ListNode<T>* PNode;
typedef ListIterator<T,Ptr,ref> Self;
public:
PNode it;
ListIterator(PNode x = nullptr)
:it(x)
{}
Ptr operator-> ()
{
return &(it->_val);
}
Ref operator* ()
{
return it->val;
}
Self& operator++ ()
{
it=it->_next;
return *this;
}
Self operator++ (int)
{
Self ret = it;
it = it-> _next;
return ret;
}
Self& operator-- ()
{
it=it->_prev;
return *this;
}
Self operator-- (int)
{
Self ret = it;
it = it-> _prev;
return ret;
}
bool operator!=(const Slef& l)
{
return it != l.it;
}
bool operator==(const Slef& l)
{
return it == l.it;
}
Self& operator=(const Slef& l)
{
it = l.it;
return *this;
}
};
反向迭代器 Reverse_iterator
以Iterator为模板,编写统一的反向迭代器模板
cpp
template<class Iterator, class Ptr, class Ref>
struct Reverse_iterator
{
Iterator _it;
typedef Reverse_iterator<Iterator, Ptr, Ref> Self;
Reverse_iterator(Iterator x)
{
_it = x;
}
Reverse_iterator() {}
bool operator!=(const Self& tmp)
{
return _it != tmp._it;
}
Ptr operator*()
{
Iterator ret = _it;
--ret;
return *ret;
}
Ref operator->()
{
return &(operator*());
}
Self& operator++()
{
--_it;
return *this;
}
Self& operator--()
{
++_it;
return *this;
}
};
管理操作 list
为用户提供函数接口。
cpp
template<class T>
class list
{
private:
typedef ListNode<T> _Node;
typedef ListNode<T>* _PNode;
_PNode _head;
size_t _size;
void empty_init()
{
_head = new _Node;
_head -> _next = _head -> _prev = nullptr;
_size = 0;
}
public:
typedef ListIterator<T,T*,T&> iterator;
typedef ListIterator<const T, const T*, const T&> const_iterstor;
typedef Reverse_iterator<iterator, T*, T&> reverse_iterator;
typedef Reverse_iterator<const_iterator, const T*, const T&> const_reverse_iterator;
reverse_iterator rbegin()
{
return reverse_iterator(end());
}
const_reverse_iterator crbegin() const
{
return const_reverse_iterator(end());
}
reverse_iterator rend()
{
return reverse_iterator(begin());
}
const_reverse_iterator crend() const
{
return const_reverse_iterator(begin());
}
void insert(iterator pos, const T& val)
{
size++;
PNode cur = new Node;
PNode next = pos.it;
PNode prev = next->_prev;
cur->_next = next;
cur->_prev = prev;
next->_prev = cur;
prev->_next = cur;
cur->_val = val;
}
iterator begin()
{
return _head->_next;
}
iterator end()
{
return _head;
}
const_iterator begin() const
{
return _head->_next;
}
const_iterator end() const
{
return _head;
}
list()
{
empty_init();
}
list(int n, const T& value = T())
{
empty_init();
while (n--)
{
push_back(value);
}
}
template <class Iterator>
list(Iterator first, Iterator last)
{
empty_init();
while (first != last)
{
push_back(*first);
++first;
}
}
list(const list<T>& l)
{
empty_init();
for (auto& e : l)
push_back(e);
}
list<T>& operator=(const list<T> l)
{
swap(l);
return *this;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
~list()
{
clear();
delete _head;
}
void swap(list<T>& l)
{
std::swap(l.size, size);
std::swap(l._head, _head);
}
void push_front(const T& val)
{
insert(begin(), val);
}
void push_back(const T& val)
{
insert(end(), val);
}
iterator erase(iterator pos)
{
size--;
PNode next = (pos.it)->_next;
PNode prev = (pos.it)->_prev;
next->_prev = prev;
prev->_next = next;
delete pos.it;
return next;
}
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
};
结语
以上就是本期的全部内容了,喜欢就多多关注吧!!!