#include <iostream>
#include <iterator>
#include <memory>
#include <algorithm>
using namespace std;
template <typename T> // 结点结构
struct __list_node
{
typedef void* void_pointer;
void_pointer prev;
void_pointer next;
T data;
};
template <typename T, typename Ref = T&, typename Ptr = T*> //迭代器
struct __list_iterator
{
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
typedef __list_iterator<T, Ref, Ptr> self;
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef __list_node<T>* link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
link_type node; //迭代器实际管理的资源指针
__list_iterator(link_type x) : node(x) {}
__list_iterator() {}
__list_iterator(const iterator &x) : node(x.node) {}
bool operator==(const self &x) const
{
return node == x.node;
}
bool operator!=(const self &x) const
{
return node != x.node;
}
reference operator*() const // 重载解引用运算符
{
return (*node).data;
}
reference operator->() const // 重载箭头运算符
{
return &(operator*());
}
self& operator++() // 前增版本
{
node = (link_type)((*node).next);
return *this;
}
self& operator++(int) // 后增版本
{
self tmp = *this;
++*this;
return *this;
}
self& operator--()
{
node = (link_type)((*node).prev);
return *this;
}
self& operator--(int)
{
self tmp = *this;
--*this;
return *this;
}
};
template <typename T, typename Alloc = allocator<T>>
class list
{
protected:
typedef void* void_pointer;
typedef __list_node<T> list_node;
//typedef allocator<T> list_node_allocator;
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef list_node* link_type; // 指针类型
typedef size_t size_type;
typedef ptrdiff_t difference_type;
public:
typedef __list_iterator<T> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
typedef reverse_iterator<const_iterator> const_reverse_iterator;
typedef reverse_iterator<iterator> reverse_iterator;
protected:
link_type get_node() { return list_node_allocator.allocate(1); } // 分配一个结点的内存 但不构造
void put_node(link_type p) { list_node_allocator.deallocate(p, 1); } // 释放指定结点内存 但不析构
link_type creat_node(const T &x) // 创建并构造一个结点
{
link_type p = get_node();
list_node_allocator.construct(&(p->data), x);
return p;
}
void destory_node(link_type p) // 析构并且释放一个结点
{
list_node_allocator.destory(&(p->data));
put_node(p);
}
protected:
void empty_initialize() //用于空链表的建立
{
node = get_node();
node->next = node;
node->prev = node;
}
void fill_initialize(size_type n, const T& value) // 创建n个值为value的结点的链表
{
empty_initialize();
insert(begin(), n, value);
}
template <typename InputIterator>
void range_initialize(InputIterator first, InputIterator last) // 创建一个由区间初始化的链表
{
empty_initialize();
insert(begin(), first, last);
}
protected:
link_type node; //指向空白尾后结点
Alloc list_node_allocator; // 管理内存分配回收
public:
list() { empty_initialize(); } // 构造一个空链表
list(size_type n, const T &value) { fill_initialize(n, value); } // 以n个相同元素初始化
explicit list(size_type n) { fill_initialize(n, T()); }
template <typename InputIterator> // 以一个区间初始化
list(InputIterator first, InputIterator last)
{
range_initialize(first, last);
}
list(const list<T, Alloc> &x) // 复制构造
{
range_initialize(x.begin(), x.end());
}
list<T, Alloc>& operator=(const list<T, Alloc>& x);
~list() // 析构函数 清空链表
{
clear();
put_node(node); // 释放尾后结点所占的内存
}
iterator begin() { return (link_type)((*node).next); }
const_iterator begin() const { return (link_type)((*node).next); }
iterator end() { return node; }
const_iterator end() const { return node; }
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
bool empty() const // 判空
{
return node->next == node;
}
size_type size() const // 调用全局函数distance() 时间复杂度:O(n)
{
size_type result = distance(begin(), end());
return result;
}
size_type max_size() const { return size_type(-1); }
reference front() { return *begin(); }
const_reference front() const { return *begin(); }
reference back() { return *(--end()); }
const_reference back() const { return *(--end()); }
void swap(list<T, Alloc> &x) // j交换指针
{
std::swap(node, x.node);
}
iterator insert(iterator position, const T &x) // 在指定位置前插入一个元素
{
link_type tmp = creat_node(x);
tmp->next = position.node;
tmp->prev = (position.node)->prev;
(link_type(position.node->prev))->next = tmp;
position.node->prev = tmp;
}
iterator insert(iterator position)
{
return insert(position, T());
}
template <class InputIterator> // 在指定位置前插入一个区间
void insert(iterator position, InputIterator first, InputIterator last);
void insert(iterator pos, size_type n, const T& x); // 在指定位置前插入n个x元素
void push_front(const T &x) { insert(begin(), x); }
void push_back(const T &x) { insert(end(), x); }
iterator erase(iterator position) // 擦除指定结点
{
link_type prev_node = (link_type)((position.node)->prev);
link_type next_node = (link_type)((position.node)->next);
prev_node->next = next_node;
next_node->prev = prev_node;
destory_node(position.node);
return iterator(next_node);
}
iterator erase(iterator first, iterator last); // 擦除一个区间
void pop_front() { erase(begin()); } // 删除链表首元结点
void pop_back() // 删除链表最后一个结点
{
iterator tmp = end();
erase(--tmp);
}
void resize(size_type new_size, const T &x); // 调整链表大小
void resize(size_type new_size) { resize(new_size, T()); }
void clear(); // 请空链表
protected:
// 将[first, last)内的元素迁移到position之前
void transfer(iterator position, iterator first, iterator last)
{
if (position != last)
{
(*(link_type((*last.node).prev))).next = position.node;
(*(link_type((*first.node).prev))).next = last.node;
(*(link_type((*position.node).prev))).next = first.node;
link_type tmp = link_type((*position.node).prev);
(*position.node).prev = (*last.node).prev;
(*last.node).prev = (*first.node).prev;
(*first.node).prev = tmp;
}
}
public:
void splice(iterator position, list& x) // 将链表x移动到pos之前 x不能和*this相同
{
if (!x.empty())
transfer(position, x.begin(), x.end());
}
void splice(iterator position, list&, iterator i) // 将i所指向元素接合于position之前 pos和i可能指向同一个list
{
iterator j = i;
++j;
if (position == i || position == j)
{
return;
}
transfer(position, i, j);
}
// 将[first,last)内的所有元素接合于pos之前, pos可能和[first,last)指向同一个list 但pos不能位于[first,last)内
void splice(iterator position, list&, iterator first, iterator last)
{
if (first != last)
{
transfer(position, first, last);
}
}
void remove(const T& value);
void unique();
void merge(list& x);
void reverse();
void sort();
//template <class Predicate> void remove_if(Predicate);
//template <class BinaryPredicate> void unique(BinaryPredicate);
//template <class StrictWeakOrdering> void merge(list&, StrictWeakOrdering);
//template <class StrictWeakOrdering> void sort(StrictWeakOrdering);
};
// 判断两个链表是否相等
template <typename T, typename Alloc>
inline bool operator==(const list<T, Alloc>& x, const list<T, Alloc>& y)
{
typedef typename list<T, Alloc>::link_type link_type;
link_type e1 = x.node;
link_type e2 = y.node;
link_type n1 = e1->next;
link_type n2 = e2->next;
for (; n1 != e1 && n2 != e2; n1 = (link_type)n1->next, (link_type)n2 = n2->next)
{
if (n1->data != n2->data) return false;
}
return n1 == e1 && n2 == e2;
}
// 字典序比较链表大小
template <typename T, typename Alloc>
inline bool operator<(const list<T, Alloc> &x, const list<T, Alloc> &y)
{
return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
}
template <typename T, typename Alloc>
inline void swap(list<T, Alloc> &x, list<T, Alloc> &y)
{
x.swap(y);
}
template <typename T, typename Alloc>
list<T, Alloc>& list<T, Alloc>::operator=(const list<T, Alloc>& x)
{
if (this != &x)
{
iterator first1 = begin();
iterator last1 = end();
const_iterator first2 = x.begin();
const_iterator last2 = x.end();
while (first1 != last1 && first2 != last2)
(*first1)++ = (*first2)++;
if (first2 == last2) // x的元素少于原list
erase(first1, last1);
else
insert(last1, first2, last2); // x的元素多余原list
}
return *this;
}
template <typename T, typename Alloc>
template <typename InputIterator>
void list<T, Alloc>::insert(iterator position, InputIterator first, InputIterator last)
{
for (; first != last; ++first)
{
insert(position, *first);
}
}
template <typename T, typename Alloc>
void list<T, Alloc>::insert(iterator posistion, size_type n, const T &x)
{
for (; n > 0; --n)
{
insert(posistion, x);
}
}
template <typename T, typename Alloc>
list<T, Alloc>::iterator list<T, Alloc>::erase(iterator first, iterator last)
{
while (first != last)
{
erase(first++);
return last;
}
}
template <typename T, typename Alloc>
void list<T, Alloc>::resize(size_type new_size, const T &x)
{
iterator i = begin();
size_type len = 0;
for (; i != end() && len < new_size; ++i, ++len);
if (len == new_size)
erase(i, end());
else
insert(end(), new_size - len, x);
}
template <typename T, typename Alloc>
void list<T, Alloc>::clear()
{
link_type cur = (link_type)node->next;
while (cur != node) // 从头结点到最后一个结点
{
link_type tmp = cur;
cur = (link_type)cur->next;
destory_node(tmp);
}
node->next = node;
node->prev = node;
}
template <typename T, typename Alloc>
void list<T, Alloc>::remove(const T &value) // 移除特定值的所有结点
{
iterator first = begin();
iterator last = end();
for (first != last)
{
iterator next = first;
++next;
if (*first == value)
erase(first);
first = next;
}
}
template <typename T, typename Alloc>
void list<T, Alloc>::unique() // 删除容器内所有相邻的重复结点
{
iterator first = begin();
iterator last = end();
if (first == last) return;
iterator next = first;
while (++next != last)
{
if (*first == *next)
{
erase(next);
}
else
{
first = next;
}
next = first;
}
}
template <typename T, typename Alloc>
void list<T, Alloc>::merge(list<T, Alloc> &x) // 将x合并到*this中去 前提是两个list分别增序排序过
{
iterator first1 = begin();
iterator last1 = end();
iterator first2 = x.begin();
iterator last2 = x.end();
while (first1 != last1 && first2 != last2)
{
if (*first1 < *first2)
{
iterator next = first2;
transfer(first1, first2, ++next);
first2 = next;
}
else
++first1;
}
if (first2 != last2)
transfer(last1, first2, last2);
}
template <typename T, typename Alloc>
void list<T, Alloc>::reverse() // 反转链表
{
if (node->next == node || link_type(node->next)->next == node)
return;
iterator first = begin();
++first;
while (first != end())
{
iterator old = first;
++first;
transfer(begin(), old, first);
}
}
// 参考 http://blog.csdn.net/chenhanzhun/article/details/39337331
// 因为list的迭代器是双向迭代器 所以不能用全局的sort函数 list内部提供了一个sort成员函数
template <typename T, typename Alloc>
void list<T, Alloc>::sort()
{
if (node->next == node || link_type(node->next)->next == node) // 如果是空链表或者仅有一个元素 就不进行任何操作
return;
list<T, Alloc> carry; // 起到搬运作用
list<T, Alloc> counter[64]; // 中转站 其中对于counter[i]里面最多的存储数据为2^(i+1)个节点 若超出则向高位进位即counter[i+1]
while (!empty())
{
carry.splice(carry.begin(), *this, begin());//把当前链表的第一个节点放在carry链表头
int i = 0;
while (i < fill && !counter[i].empty())
{
counter[i].merge(carry); // 把链表carry合并到counter[i]
carry.swap(counter[i++]);//交换链表carry和counter[i]内容
}
carry.swap(counter[i]);//交换链表carry和counter[i]内容
if (i == fill) ++fill;
for (int i = 1; i < fill; ++i) counter[i].merge(counter[i - 1]); //把低位不满足进位的剩余数据全部有序的合并到上一位
swap(counter[fill - 1]);//最后把已排序好的链表内容交换到当前链表
}
}