#include <list>
#include <iostream>
using std::list; /*
双向环状链表
//每一个结点 一个数据域 一个前驱指针 一个后驱指针
随机插入方便0(1) 随机访问效率低0(n)
*/ bool foo(int a) {
return a % == ;
} bool foo2(int a) {
return a == ;
} int main()
{
//初始化列表
list<int> list_test1;
list<int> list_test2();//5个结点的链表 默认值是0
list<int> list_test3(,);
list<int> list_test4(list_test3);
list<int> list_test5= list_test4; //末尾插入
list_test1.push_back();
list_test1.push_back();
list_test1.push_back(); //末尾删除
list_test1.pop_back(); //和vector的区别
//排序
list_test1.sort();
//前侧插入
list_test1.push_front();
//前侧删除
list_test1.pop_front(); //merge 合并两个有序链表再排序
list_test1.merge(list_test2); //清除指定值的元素 remove
list_test1.remove(); //清除满足条件的元素 remove_if 参数是返回bool的函数指针
list_test1.remove_if(foo);
list_test1.remove_if(foo2);//条件返回值必须是bool //splice 拼接结合 //unique() 删除重复值,保证唯一
list_test1.unique(); for (auto& i : list_test1) {
std::cout << i << std::endl;
}
}

自写

#pragma once
#include <memory> using namespace std;
/*
list结点 |next|---->|next|---->|next|
|prev|<----|prev|<----|prev|
|data| |data| |data| */
template <typename T>
struct __list_node
{
typedef __list_node* list_node_pointer;
list_node_pointer next;
list_node_pointer prev;
T data;
}; /*
list迭代器 操作容器
*/
template <typename T>
struct __list_iterator
{
typedef __list_iterator<T> self;
typedef __list_node<T> * link_type; link_type ptr;//指向list结点的指针 头指针 __list_iterator(link_type p = nullptr) :ptr(p) {}//构造
__list_iterator(const self& that) :ptr(that.ptr) {}//拷贝构造 bool operator==(const self& that) const
{
return this->ptr == that.ptr;
}
bool operator!=(const self& that) const
{
return this->ptr != that.ptr;
}
T& operator*()const
{
return ptr->data;
}
T* operator->()const
{
return &(operator*());
}
//迭代器前++ 返回下一个结点
self& operator++()
{
ptr=ptr->next;//指针指向下一个结点
return *this;
} //后++ 先返回后自增
self operator++()
{
self tmp = *this;
++*this;//调用了前面写的前++的操作
return tmp;
} //迭代器前-- 返回上一个
self& operator--()
{
ptr = ptr->prev;//指针指向上一个结点
return *this;
} //后-- 先返回后自减
self operator--()
{
self tmp = *this;
--*this;
return tmp;
}
}; /*
list
*/
template <typename T>
class MyList
{
protected:
typedef __list_node<T> list_node;
//空间配置器 内存池实现小块内存的分配机制
/*
大概实现
一级空间配置器 直接封装了malloc free
二级空间配置器 内存池 自有链表
实现原理 用户申请内存》128? 一级:二级 */
allocator<list_node> nodeAllocator; typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef size_t size_type; public:
typedef list_node* link_type;
typedef __list_iterator<T> iterator; protected:
//只要一个结点指针可以表示整个list
link_type node; //指向list结点的指针 头指针 //空间配置器
//分配新节点(内存) 不会进行构造
list_node alloc_node()
{
return = nodeAllocator.allocate(sizeof(list_node));
} //释放结点不会进行析构
void dealloc_node(list_node p)
{
nodeAllocator.deallocate(p);
} //分配新结点(内存) 用value构造新节点里面内容
list_node alloc_dtor_node(const T& value)
{
//分配结点空间
link_type p = alloc_node();
//构造结点内容
nodeAllocator.construct(&p->data, value);
} //析构整个结点 释放内存
void dealloc_dtor_node(list_node p)
{ } //空的初始化
void empty_init()
{
node = alloc_node();//分配一个结点空间 让node指向他
node->prev = node;//指向他自己
node->next = node;
}
public:
MyList()//链表初始化
{
empty_init()
}
~MyList(); iterator begin()
{
return node->next;
}
iterator begin() const
{
return node->next;
}
iterator end()
{
return node;
}
iterator end() const
{
return node;
} bool empty()const
{
return node == node->next;//结点指向他自身
//return node== node->prev;
} iterator insert(iterator pos, const T& value)
{
//先创造一个结点
link_type tmp = alloc_dtor_node(value);
//寻找位置插入
node->next = pos.node;
node->prev = pos.prev; pos.node->prev->next = tmp;
pos.node->prev = tmp; return tmp
} void push_back(const T& value)
{
insert(end(), value);
}
private: };

list 链表的更多相关文章

  1. Redis链表实现

    链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...

  2. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  3. 排序算法----基数排序(RadixSort(L))单链表智能版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  4. 防御性编程习惯:求出链表中倒数第 m 个结点的值及其思想的总结

    防御性编程习惯 程序员在编写代码的时候,预料有可能出现问题的地方或者点,然后为这些隐患提前制定预防方案或者措施,比如数据库发生异常之后的回滚,打开某些资源之前,判断图片是否存在,网络断开之后的重连次数 ...

  5. 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法

    有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...

  6. C语言之链表list

    #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h& ...

  7. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

  8. 学习javascript数据结构(二)——链表

    前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...

  9. 用JavaScript来实现链表LinkedList

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...

  10. 数据结构:队列 链表,顺序表和循环顺序表实现(python版)

    链表实现队列: 尾部 添加数据,效率为0(1) 头部 元素的删除和查看,效率也为0(1) 顺序表实现队列: 头部 添加数据,效率为0(n) 尾部 元素的删除和查看,效率也为0(1) 循环顺序表实现队列 ...

随机推荐

  1. 同时安装CUDA8.0和CUDA9.0

    http://geyao1995.com/CUDA8_CUDA9/ tensorflow1.5版本竟然不支持CUDA8.0了 卸载是不可能卸载的 1.原料准备 CUDA9.0下载:https://de ...

  2. 洛谷P2015 二叉苹果树(树状dp)

    题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...

  3. 集合类中的Collection接口实现类

    今天学习一下集合包里面的内容,常见的有Collection和Map两个接口的实现类Collection中常见的又分为两种: 1.List ,支持放入重复的对象,实现类有arraylist,linked ...

  4. python数据结构:进制转化探索

    *********************************第一部分*************************************************************** ...

  5. CentOS 安装MySQL5.7 源码方式安装

    MySQL rpm方式安装:https://www.cnblogs.com/deverz/p/9560403.html 1.卸载已经安装的MySQL yum list installed mysqlr ...

  6. STemWin5.22移植笔记【转】

    来自:http://www.openedv.com/posts/list/27697.htm STemWin5.22移植笔记 网上关于emwin的资料很少,我在移植的时候查了很多资料,对我一个感觉是好 ...

  7. %matplotlib inline 被注释掉后,pycharm不能生成图

    目录 问题描述 解决方案 @ 问题描述 在 jupyter 编译器中 程序的开头,有这么一行 %matplotlib inline import numpy as np import matplotl ...

  8. Eureka 系列(02)Eureka 一致性协议

    目录 Eureka 系列(02)Eureka 一致性协议 0. Spring Cloud 系列目录 - Eureka 篇 1. 服务发现方案对比 1.1 技术选型 1.2 数据模型 2. Eureka ...

  9. Scrapy框架: middlewares.py设置

    # -*- coding: utf-8 -*- # Define here the models for your spider middleware # # See documentation in ...

  10. python- 属性 静态方法,类方法

    一,面向对象结构与成员 1,1 面向对象结构分析: 那么每个大区域又可以分为多个小部分: class A: company_name = '老男孩教育' # 静态变量(静态字段) __iphone = ...