从零开始写STL-容器-list

  • List 是STL 中的链表容器,今天我们将通过阅读和实现list源码来解决一下问题:
  • List内部的内存结构是如何实现的?
  • 为什么List的插入复杂度为O(1)?
  • 为什么List的size()函数复杂度为O(n)?

list 容器的幕后英雄 - list 结点

作为一个链表首先要维护数据(模板元素的实例内容),指向前一个节点的指针 和 指向后一个节点的指针。List 的 结点作为list 容器中 元素内容 和 容器组织逻辑的一个中间层。

  • 为什么不能搞成单向链表?

    如果单向链表的话,请您试想一下要删除迭代器pos指向的元素应该怎么操作?我们必须知道前驱后继才能正确进行删除
for(auto it = begin(); it != end(); it++)
{
if(it->next == pos)
{
//...逻辑代码
}
}

这样的单向链表每次寻找一个节点的前驱 后继 都要经过最坏复杂度为O(n)的查询,所有应该实现为双向链表。

list 容器的 迭代器

  • 维护一个指向list_node 的指针
	template<class T>
struct list_iterator : public bidirectional_iterator<T>
{}//继承bidirectional_iterator类 便于类型萃取 和 特定算法应用

list 容器的 逻辑结构

  • 环状链表

    node 表示尾部的一个空白结点,其next 指向list的头节点, pre指向list的尾节点

为什么这样设计?

首先list作为双向链表需要提供向前和向后的迭代能力,这样设计可以在O1的时间获得首尾元素,而且可以避免链表为空时的边界检查(只需要看一下node->pre == node,则为空)

		node_ptr get_node()
{
return data_allocator.allocate(1);
}
node_ptr new_node(const T& x)
{
node_ptr p = get_node();
construct(&p->data, x);
p->next = p->pre = nullptr;
return p;
}

构造函数

		list()
{
//注意 typedef std::allocator<list_node<T>> Data_allocator;
node.ptr = data_allocator.allocate(1);
node.ptr->next = node.ptr->pre = node.ptr;
}
list(const self& rhs) :list(rhs.begin(), rhs.end())
{ }
list(std::initializer_list<T> li):list(li.begin(),li.end())
{ }
template<class c>
list(const c l,const c r)
{
node.ptr = data_allocator.allocate(1);
node.ptr->next = node.ptr->pre = node.ptr;
for (auto it = l; it != r; it++)
{
push_back((*it));//逐个插入到list的末端
}
}
list(size_type n, const T& val)
{
node.ptr = data_allocator.allocate(1);//初始化node
node.ptr->next = node.ptr->pre = node.ptr;
while (n--)
push_back(val);
}

析构函数

遍历列表 销毁数据实例

		~list()
{
if (!empty())
{
for (iterator it = begin(); it != end(); )
{
data_allocator.destroy(&it.ptr->data);
it++;
}
}
}

Modify

  • 注意指针操作的先后顺序
		void push_front(const T& x)
{
node_ptr p = new_node(x);
node.ptr->next->pre = p;
p->next = node.ptr->next;
node.ptr->next = p;
p->pre = node.ptr;
}
void push_back(const T& x)
{
node_ptr p = new_node(x);
node.ptr->pre->next = p;
p->pre = node.ptr->pre;
p->next = node.ptr;
node.ptr->pre = p;
}
void pop_front()
{
node_ptr tmp = node.ptr->next;
node.ptr->next = node.ptr->next->next;
node.ptr->next->pre = node.ptr;
data_allocator.deallocate(tmp,sizeof(list_node));
}
void pop_back()
{
node_ptr tmp = node.ptr->pre;
node.ptr->pre = tmp->pre;
tmp->pre->next = node.ptr;
data_allocator.deallocate(tmp, sizeof(list_node));
}
iterator erase(iterator pos)
{
pos.ptr->pre->next = pos.ptr->next;
pos.ptr->next->pre = pos.ptr->pre;// 前驱 后继 结点的指针操作
node_ptr tmp = pos.ptr->next;
destroy(&pos.ptr->data);//销毁
data_allocator.deallocate(pos.ptr,sizeof(list_node));// 回收内存
return iterator(tmp);
}
iterator erase(iterator first, iterator last)
{
first.ptr->pre->next = last.ptr;
last.ptr->pre = first.ptr->pre;
for (auto it = first; it != last; it++)
destroy(&it.ptr->data);
return first;
}
//The list container is extended by inserting new elements before the element at position.
iterator insert(iterator pos, const T& x)
{
node_ptr p = new_node(x);
pos.ptr->pre->next = p;
p->pre = pos.ptr->pre;
p->next = pos.ptr;
pos.ptr->pre = p;
return pos;
} void insert(iterator pos, size_type sz, const T& x)
{
while (sz--)
insert(pos, x);
}

Splice 函数

		/* 将 first到last的元素移动到 pos 之前 */
void transfer(iterator pos, iterator first, iterator last)
{
if (pos != last)
{
last.ptr->pre->next = pos.ptr;
first.ptr->pre->next = last.ptr;
pos.ptr->pre->next = first.ptr;
auto tmp = pos.ptr->pre;
pos.ptr->pre = last.ptr->pre;
last.ptr->pre = first.ptr->pre;
first.ptr->pre = tmp;
}
}
void splice(iterator pos, list<T>& rhs)
{
if (*this != rhs)
transfer(pos, rhs.begin(), rhs.end());
}
void splice(iterator pos, list<T>& rhs, iterator first, iterator last)
{
if (*this != rhs)
{
transfer(pos, first, last);
}
}
void splice(iterator pos, list<T>& rhs, iterator it)
{
it.ptr->pre->next = it.ptr->next;
it.ptr->next->pre = it.ptr->pre;
pos.ptr->pre->next = it.ptr;
it.ptr->pre = pos.ptr->pre;
pos.ptr->pre = it.ptr;
it.ptr->next = pos.ptr;
}

merge 函数

注意链表的merge函数 和算法库中含义有所不同,在这里会将参数链表全部合并进来(也就是说在调用完这个函数之后,other 参数应该是空)

		void merge(list<T> &other)
{
auto p1 = begin(), p2 = other.begin();
while (p1 != end() && p2 != other.end())
{
if (*p1 < *p2)
p1++;
else if (*p1 >= *p2)
{
auto tmp = p2;// 注意保存迭代器下一个位置,如果不保存,直接再调用splice之后p2++会怎样?首先p2!=other.end()这个条件永远不会触发,因为p2已经到了this的链表中
tmp++;
splice(p1, other, p2);
p2 = tmp;
}
}
if (!other.empty()) {
splice(end(), other);
}
}

list 归并排序的非递归形式(难点)

非递归形式的 归并排序,因为List并不支持随机存取迭代器,sort不能用于排序list。

归并的思路如下:先建立64个桶,然后从前向后遍历,每次从要排序的list头取出一个元素插入桶中,第一个桶要存的元素最多为1个,第二个桶要存的最多为2个,第K个桶最多为2^K个元素。在插入元素的时候从前向后插入,如果到达当前桶的上限就向后归并。

具体的算法实现极为巧妙!

个人感觉参考了位运算的进位性质

		void sort()
{
if (size() <= 1)
{
return;
}
self carry;
self counter[64];
int fill = 0;
while (!empty())
{
carry.splice(carry.begin(), *this, begin());
int i = 0;
while (i < fill && !counter[i].empty())
{
counter[i].merge(carry);
carry.swap(counter[i++]);
}
carry.swap(counter[i]);
if (i == fill)
{
++fill;
}
}
for (int i = 1; i < fill; i++)
{
counter[i].merge(counter[i - 1]);
}
swap(counter[fill - 1]);
}

从零开始写STL-容器-list的更多相关文章

  1. 从零开始写STL—容器—vector

    从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...

  2. 从零开始写STL - 智能指针

    从零开始写STL - 智能指针 智能指针的分类及其特点: scoped_ptr:初始化获得资源控制权,在作用域结束释放资源 shared_ptr: 引用计数来控制共享资源,最后一个资源的引用被释放的时 ...

  3. 从零开始写STL—栈和队列

    从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...

  4. 从零开始写STL—模板元编程之any

    any class any; (since C++17) The class any describes a type-safe container for single values of any ...

  5. 从零开始写STL—functional

    function C++11 将任意类型的可调用(Callable)对象与函数调用的特征封装到一起. 这里的类是对函数策略的封装,将函数的性质抽象成组件,便于和algorithm库配合使用 基本运算符 ...

  6. 从零开始写STL—哈希表

    static const int _stl_num_primes = 28; template<typename T, typename Hash = xhash<T>> cl ...

  7. 从零开始写STL—模板元编程之tuple

    tuple Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generali ...

  8. 从零开始写STL—set/map

    这一部分只要把搜索树中暴露的接口封装一下,做一些改动. set源码剖析 template<typename T> class set { public: typedef T key_typ ...

  9. STL容器

    啦啦啦,今天听啦高年级学长讲的STL容器啦,发现有好多东西还是有必要记载的,毕竟学长是身经百战的,他在参加各种比赛的时候积累的经验可不是一天两天就能学来的,那个可是炒鸡有价值的啊,啊啊啊啊啊 #inc ...

随机推荐

  1. ES6知识点汇总

    MDN镇楼: https://developer.mozilla.org/zh-CN/ 1.ES6新添加数据类型:symbol  -----------   https://developer.moz ...

  2. XML读取的小例子

    public void CalculateLeave(string userAcount, string xml) //传过来的是xml内容 { try { var xmlDoc = new Syst ...

  3. ios开发介绍

    iOS开发概述 •什么是IOS •什么是IOS开发 •为什么要选择IOS开发 •学习IOS开发的准备   1.什么是iOS   •iOS是一款由苹果公司开发的操作系统(OS是Operating Sys ...

  4. CentOS 6.4 php-fpm 添加service 添加平滑启动/重启

    nginx通过FastCGI运行PHP比Apache包含PHP环境有明显的优势,最近有消息称,PHP5.4将很有可能把PHP-FPM补丁包含在内核里,nginx服务器平台上运行PHP将更加轻松,下面我 ...

  5. (转)淘淘商城系列——zookeeper单机版安装

    http://blog.csdn.net/yerenyuan_pku/article/details/72717744 这篇文章,我单独来说一下zookeeper如何安装.在实际开发中,zookeep ...

  6. JavaScipt30(第二十二个案例)(主要知识点:getBoundingClientRect)

    这是第二十二个案例,这个例子实现的是鼠标移入a标签时,将其高亮. 附上项目链接: https://github.com/wesbos/JavaScript30 以下为注释后的源码: <scrip ...

  7. Android全局异常捕获

    PS:本文摘抄自<Android高级进阶>,仅供学习使用 Java API提供了一个全局异常捕获处理器,Android引用在Java层捕获Crash依赖的就是Thread.Uncaught ...

  8. 第1节 MapReduce入门:11、mapreduce程序的入门

    1.1.理解MapReduce思想 MapReduce思想在生活中处处可见.或多或少都曾接触过这种思想.MapReduce的思想核心是“分而治之”,适用于大量复杂的任务处理场景(大规模数据处理场景). ...

  9. DHCP和PXE

    继续学习,Let's go!DHCP和PXE都是什么呢?如果非科班出身,可能一脸懵逼,好多东西需要去学习了,真的,继续学吧,付出不一定会有回报,不付出肯定就是等死了,呵呵! 一.DHCP 真正需要手动 ...

  10. C++ 之 string

    C++ 的 string 类封装了很多对字符串的常用操作. string 类是模板类 basic_string类,以 char作为其元素类型的类. string 以单字节作为一个字符,如果处理多字符集 ...