Mylist 笔记

#if 0//list
#include<iostream>
#include<list>
#include<map>

using namespace std;
int main()
{
	list<int> list_test1;             //初始化空链表
	list<int> list_test2(5);          //初始化5个元素默认值是0的链表
	list<int> list_test3(5, 2);        //初始化5个元素默认值是2的链表
	list<int> list_test4(list_test1); //拷贝list_test1;
	list<int> list_test5(list_test1.begin(), list_test1.end());
	//添加
	list_test1.push_back(10);
	list_test1.push_back(20);
	list_test1.push_back(30);
	//在list中没有capacity()和reserve();
	//在list中没有[] at访问;

	//reverse 翻转
	list_test1.reverse();//在vector中没有此成员函数	
	//sort()排序
	list_test1.sort();//在vector中没有此成员函数	

	//头插,删除
	list_test1.push_front(80);
	list_test1.push_front(90);
	list_test1.pop_front();
	//merge 合并两个有序的链表并排序
	list_test1.clear();
	list_test2.clear();
	list_test1.push_back(3);
	list_test1.push_back(6);
	list_test2.push_back(2);
	list_test2.push_back(4);
	list_test1.merge(list_test2);
	//remove() 清除匹配的所有元素
	list_test1.push_back(4);
	list_test1.push_back(4);
	list_test1.push_back(4);
	list_test1.remove(4);

	//remove_if()  清楚满足条件的元素
	list_test1.remove_if([](int x){return x % 2 == 0; });
	//splice()  结合
	//list_test1.splice();
	//unique()  删除重复值
	list_test1.push_back(4);
	list_test1.push_back(4);
	list_test1.push_back(4);
	list_test1.push_back(4);
	list_test1.push_back(2);
	list_test1.push_back(2);
	list_test1.unique();
}
#endif//list
#if 0
#include<iostream>
#include<deque>

using namespace std;

/*
// deque 双端队列 全名(double ended queue)
是一种双向开口的连续线性空间 允许头和尾两端操作

优点  随机访问方便  支持 [] and  at 访问
随机插入删除方便  两端都可以push and pop
缺点  占用内存多

//增删改  查
vector VS list VS deque
1、需要高效的随机存取,不在乎随机插入和删除的效率   vector
2、需要高效的插入和删除,不在乎随机存取             list
3、需要高效的增删查改,不在乎内存                   deque

//各自迭代器的比较
vector和deque迭代器支持算术运算符  list只能进行++/--操作,不支持普通的算数运算,
向量的iterator使用后就释放 但是链表list不同,迭代器使用之后还能继续用

*/
int main()
{
	deque<double> deu_test1;
	deque<double>::iterator it;
	deque<double>::iterator it1;
}
#endif//deque

头文件:#include"mylist.h"

#pragma once
template<typename _Tonde>
struct __list_node//list结点的结构
{
	typedef void* void_pointer;//重定义void*
	void_pointer next;//后指针
	void_pointer prev;//前指针
	_Tonde date;//存放数据
};

//list迭代器
template<class T,class Ref,class Ptr>
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;//self表示list结点
	typedef std::bidirectional_iterator_tag iterator_category;//双向迭代器iterator_category函数提供返回类型
	typedef T value_type;//泛型数据类型
	typedef Ref reference;//泛型引用
	typedef Ptr pointer;//泛型指针
	typedef __list_node<T>* link_type;//list结点
	typedef size_t  size_type;//运算符 sizeof 的结果。
	typedef ptrdiff_t differebce_type;//两个指针相减的结果。

	//指向list结点
	link_type node;

	__list_iterator() :node();
	__list_iterator(link_type x) :node(x){};
	__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; }//指向数据
	pointer operator->()const{ return &operator*(); }

	self&operator++()
	{
		node = (link_type)(*node).next;
		return *this;
	}
	self&operator++(int)
	{
		self tmp = *this;
		++*this;
		return tmp;
	}
	self&operator--()
	{
		node = (link_type)(*node).prev;
		return *this;
	}
	self&operator--(int)
	{
		self tmp = *this;
		--*this;
		return tmp;
	}
};

//list双向循环列表
//allocator
//空间适配器 内存池实现小块内存分配
//解决内存碎片问题
//大概原理  一级空间适配器	直接封装malloc free;
//          二级空间适配器  内存池  自由链表
//实现原理  用户申请 > 123 ? 一级:二级

template <typename _Ty, class _ALLOC = std::allocator<_Ty>>
class Mylist
{
protected:
	typedef __list_node<_Ty>list_node;
public:
	typedef __list_node* link_type;
protected:
	link_type node;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳一航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值