当前位置: 首页 > news >正文

【listlist模拟】

list&list模拟

  • 1.list使用
  • 2、list模拟
  • 附录

1.list使用

 list常见接口不做介绍,跟前面vector有相似之处,跟数据结构list基本一样。
alt
 因为list使用带头的双向循环链表实现的,不能用小标访问,只能用迭代器或范围for访问
alt

 list有成员函数sort,来实现排序
alt
 void unique(),去重,去重先先要排序

void test_list1()
{list<int> lt1 = { 10,2,3,3,4,5,6 };list<int> ::iterator it = lt1.begin();while (it != lt1.end()){cout << *it << " ";it++;}cout << endl;//list不支持sort//sort(lt1.begin(), lt1.end(), greater<int>());//list有自己的排序算法lt1.sort();it = lt1.begin();while (it != lt1.end()){cout << *it << " ";it++;}cout << endl;lt1.sort(greater<int>());it = lt1.begin();while (it != lt1.end()){cout << *it << " ";it++;}cout << endl;//unique 去重//先排序,在去重lt1.unique();for (auto e : lt1){cout << e << " ";  //10 6 5 4 3 2}cout << endl;
}

alt
 splice粘接
 void splice(const _iterator position,list& x);把x粘接到pos位置。

void test_list2()
{//splice:粘接std::list<int> mylist1, mylist2;std::list<int>::iterator it;// set some initial values:for (int i = 1; i <= 4; ++i)mylist1.push_back(i);      // mylist1: 1 2 3 4for (int i = 1; i <= 3; ++i)mylist2.push_back(i * 10);   // mylist2: 10 20 30it = mylist1.begin();++it;                         // points to 2mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4// mylist2 (empty)// "it" still points to 2 (the 5th element
}

void splice(const_iterator position,list& x,const_itrator i);
把list的第i个位置的数粘接到position位置.

void test_list3()
{list<int> mylist;for (int i = 1; i <= 4; i++){mylist.push_back(i);  //mylist:   1 2 3 4} //想把3转移到头list<int>::iterator  it = find(mylist.begin(), mylist.end(), 3);//void splice (const_iterator position, list& x, const_iterator i);//把list的第i个位置的数粘接到position位置mylist.splice(mylist.begin(), mylist, it);
}

2、list模拟

 先写申请一个节点的类,把节点弄成一个类,相当与搞了一个新的数据类型。

//这是第一个类 ListNode
//所谓的类就是把成员对象和成员函数封装在一起.
//这个全部弄成共有
//供后面存储数据来用
template<class T>
struct ListNode
{ListNode<T>* _next;ListNode<T>* _prev;T _data;//const T& data是接受常量//不能权限放大,会报错ListNode(const T& data = T()):_next(nullptr),_prev(nullptr),_data(data){}
};

 先写list的大框架

template<class T>
class list
{typedef ListNode<T> Node;
public://无参构造函数就是默认构造list(){_head = new Node;_head->_prev = _head;_head->_next = _head;}void push_back(const T& data){//...}
private:Node* _head;
};

 写push_back()先跑通框架

void push_back(const T& data)
{Node* newnode = new Node(data);Node* tail = _head->_prev;//tail newnode  _headtail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;
}

alt
 带头双向循环链表,非常简单,写尾插即可,通过_head->_prev找到尾巴,尾巴,新节点,头插入即可。


 然后把节点封装成迭代器。把节点写成一个类,封装成迭代器所具有的属性,即可。先写·迭代器的行为,在进行封装。

void list_test1()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);list<int>::iterator it = lt1.begin();while (it != lt1.end()){cout << (*it) << " ";++it;}
}

 通过iterator it = it1.begin(),定义一个对象it,把it初始化为第一个节点的位置,然后进行以下运算符重载,!=,前置++,*就可以了。

template<class T>
class ListIterator
{typedef ListIterator<T> Self;typedef ListNode<T> Node;
public:ListIterator(Node* node = T()){_node = node;}//拷贝构造不要//赋值也不要T& operator*(){return _node->_data;}bool operator!=(const Self& it){return _node != it._node;}Self& operator++(){_node = _node->_next;return *this;}
public:Node* _node;
};

 const_iterator可以通过*的运算符重载的返回值+const实现,具体在增加一个模版参数即可,不过多叙述

//Ref引用
//Ptr指针
template<class T,class Ref,class Ptr>
struct  ListIterator
{typedef ListNode<T> Node;typedef ListIterator<T,Ref,Ptr> Self;public:Node* _node;ListIterator(Node* node):_node(node){}//拷贝构造和赋值写也可以,不写也可以//这儿是浅拷贝ListIterator(const Self& it){_node = it._node;}//++itSelf& operator++(){_node = _node->_next;return *this;}Self& operator++(int){Self tmp(*this);_node = _node->_next;return tmp;}Self& operator--(){_node = _node->_prev;return *this;}Self& operator--(int){Self tmp(*this);_node = _node->_prev;return tmp;}//T* operator->()//{//	return &(_node->_data);//}Ptr operator->(){return &(_node->_data);}////这个是前置//T& operator*()//{//	return _node->_data;//}//这个是前置Ref operator*(){return _node->_data;}bool operator!=(const Self& it){return _node != it._node;}bool operator==(const Self& it){return _node == it._node;}
};

 写insert,iterator insert(iterator pos,const T& x);
在pos前面插入,返回的是x所在节点的位置

//在pos前面插入
//没有迭代器失效
iterator insert(iterator pos, const T& x)
{Node* next = pos._node;Node* newnode = new Node(x);Node* prev = next->_prev;prev->_next = newnode;newnode->_prev = prev;newnode->_next = next;next->_prev = newnode;return iterator(newnode);
}

 写erase,iterator erase(iterator pos),防止迭代器失效,返回的是pos的下一个位置

//erase后pos失效了,pos指向节点被释放了
iterator erase(iterator pos)
{assert(pos != end());Node* cur = pos._node;Node* next = cur->_next;Node* prev = cur->_prev;prev->_next = next;next->_prev = prev;delete cur;return iterator(next);
}

 写void clear(),清空,只剩头结点

void clear()
{list<T>::iterator it = begin();while (it != end()){//防止迭代器失效it = erase(it);}
}

 写~list(),先clear,在释放头节点

~list()
{//Node* cur = _head->_next;//while (cur != _head)//{//	Node* next = cur->_next;//	delete cur;//	cur = next;//}//delete _head;//_head = nullptr;clear();delete _head;_head = nullptr;
}

 写voId init_empty()保证有头结点,在后面插入数据

void empty_init()
{_head = new Node;_head->_next = _head;_head->_prev = _head;
}

 写拷贝构造,initializer_list< T >为参数的构造函数

//拷贝构造
//lt2(lt1)
list(const list<T>& lt)
{//引入这个是弄个头结点empty_init();for (const auto& e : lt){push_back(e);}
}list(initializer_list<T>il)
{empty_init();for (const auto& e : il){push_back(e);}
}
//lt1 = lt3
//lt所在的
list<T>& operator= (list<T>lt)
{std :: swap(_head,lt._head);return *this;
}

 再说一下list& operator= (listlt),注意形参是拷贝构造的一份,和赋值_head,把头地址交换即可,lt在栈桢销毁时,就会销毁交换的那个_head.

附录

#define  _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <iostream>
#include <list>
#include <algorithm>
#include <assert.h>
using namespace std;namespace wyj
{//这是第一个类 ListNode//所谓的类就是把成员对象和成员函数封装在一起.//这个全部弄成共有//供后面存储数据来用template<class T>struct ListNode{ListNode<T>* _next;ListNode<T>* _prev;T _data;//const T& data是接受常量//不能权限放大,会报错ListNode(const T& data = T()):_next(nullptr),_prev(nullptr),_data(data){}};//Ref引用//Ptr指针template<class T,class Ref,class Ptr>struct  ListIterator{typedef ListNode<T> Node;typedef ListIterator<T,Ref,Ptr> Self;public:Node* _node;ListIterator(Node* node):_node(node){}//拷贝构造和赋值写也可以,不写也可以//这儿是浅拷贝ListIterator(const Self& it){_node = it._node;}//++itSelf& operator++(){_node = _node->_next;return *this;}Self& operator++(int){Self tmp(*this);_node = _node->_next;return tmp;}Self& operator--(){_node = _node->_prev;return *this;}Self& operator--(int){Self tmp(*this);_node = _node->_prev;return tmp;}//T* operator->()//{//	return &(_node->_data);//}Ptr operator->(){return &(_node->_data);}////这个是前置//T& operator*()//{//	return _node->_data;//}//这个是前置Ref operator*(){return _node->_data;}bool operator!=(const Self& it){return _node != it._node;}bool operator==(const Self& it){return _node == it._node;}};//template<class T>//class ListConstIterator//{//	typedef ListNode<T> Node;//	typedef ListConstIterator<T> Self;//	Node* _node;//public://	ListConstIterator(Node* node)//		:_node(node)//	{}//	//拷贝构造和赋值写也可以,不写也可以//	//这儿是浅拷贝//	ListConstIterator(const Self& it)//	{//		_node = it._node;//	}//	//++it//	Self& operator++()//	{//		_node = _node->_next;//		return *this;//	}//	Self& operator++(int)//	{//		Self tmp(*this);//		_node = _node->_next;//		return tmp;//	}//	Self& operator--()//	{//		_node = _node->_prev;//		return *this;//	}//	Self& operator--(int)//	{//		Self tmp(*this);//		_node = _node->_prev;//		return tmp;//	}//	const T* operator->()//	{//		return &(_node->_data);//	}//	//这个是前置//	const T& operator*()//	{//		return _node->_data;//	}//	bool operator!=(const Self& it)//	{//		return _node != it._node;//	}//	bool operator==(const Self& it)//	{//		return _node == it._node;//	}//};template<class T >class list{//封装成私有,只能list内部用typedef ListNode<T> Node;public://ListIterator通过传节点对节点进一步处理//typedef ListIterator<T> iterator;//typedef ListConstIterator<T> const_iterator;typedef ListIterator<T,T&,T*> iterator;typedef ListIterator<T,const T&,const T*> const_iterator;iterator begin(){//iterator it(_head->_next);//return it;//匿名对象//iterator这个类把这个节点封装了,通过传这个节点给iterator//调用iterator的构造函数,把节点给iterator中的_nodereturn iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator begin()  const{//iterator it(_head->_next);//return it;//匿名对象return const_iterator(_head->_next);}const_iterator end()  const{return const_iterator(_head);}void empty_init(){_head = new Node;_head->_next = _head;_head->_prev = _head;}list(){_head = new Node;_head->_next = _head;_head->_prev = _head;// empty_init()}//拷贝构造//lt2(lt1)list(const list<T>& lt){//引入这个是弄个头结点empty_init();for (const auto& e : lt){push_back(e);}}list(initializer_list<T>il){empty_init();for (const auto& e : il){push_back(e);}}//lt1 = lt3//lt所在的list<T>& operator= (list<T>lt){std :: swap(_head,lt._head);return *this;}~list(){//Node* cur = _head->_next;//while (cur != _head)//{//	Node* next = cur->_next;//	delete cur;//	cur = next;//}//delete _head;//_head = nullptr;clear();delete _head;_head = nullptr;}void clear(){list<T>::iterator it = begin();while (it != end()){//防止迭代器失效it = erase(it);}}void push_back(const T& x){//Node* newnode = new Node(x);//Node* tail = _head->_prev;////tail->_next = newnode;//newnode->_prev = tail;//newnode->_next = _head;//_head->_prev = newnode;insert(end(), x);}void pop_back(){erase(--end());}void push_front(const T& x){insert(begin(), x);}void pop_front(){erase(begin());}//在pos前面插入//没有迭代器失效iterator insert(iterator pos, const T& x){Node* next = pos._node;Node* newnode = new Node(x);Node* prev = next->_prev;prev->_next = newnode;newnode->_prev = prev;newnode->_next = next;next->_prev = newnode;return iterator(newnode);}//erase后pos失效了,pos指向节点被释放了iterator erase(iterator pos){assert(pos != end());Node* cur = pos._node;Node* next = cur->_next;Node* prev = cur->_prev;prev->_next = next;next->_prev = prev;delete cur;return iterator(next);}private:Node* _head;};void func(const list<int>& lt){list<int>::const_iterator it = lt.begin();while (it != lt.end()){cout << (*it) << " ";++it;}cout << endl;}void test_list1(){list<int>l1;l1.push_back(1);l1.push_back(2);l1.push_back(3);l1.push_back(4);l1.push_back(5);//调用begin,把节点给iterator,用iterator来接受list<int>::iterator it = l1.begin();while (it != l1.end()){cout << (*it) << " ";*it += 10;++it;}cout << endl;for (auto e : l1){cout << e << " ";++it;}cout << endl;}struct Pos{Pos(int row =0,int col = 0):_row(row),_col(col){}int _row;int _col; };void test_list2(){list<Pos>lt1;lt1.push_back(Pos(100, 100));lt1.push_back(Pos(200, 200));lt1.push_back(Pos(300, 400));list<Pos>::iterator it = lt1.begin();while (it != lt1.end()){//为了可读性,省略了一个箭头cout << it->_row << ":"<<it->_col<<endl;//cout << it.operator->()->_row << ":" << it.operator->()->_col << endl;++it;}}void test_list3(){list<int>l1;l1.push_back(1);l1.push_back(2);l1.push_back(3);l1.push_back(4);l1.push_back(5);func(l1);cout << endl;l1.push_front(10);l1.push_front(10);l1.push_front(10);func(l1);cout << endl;l1.pop_front();l1.pop_front();func(l1);cout << endl;l1.pop_back();l1.pop_back();func(l1);cout << endl;}void test_list4(){list<int>lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);func(lt1);list<int> lt2(lt1);func(lt2);list<int> lt3;lt3 = lt1;func(lt2);}void test_list5(){list<int> lt1 = { 1,2,3,4,5,6 };func(lt1);}
}
#include "list.h"
#include "list1.h"
void test_list1()
{list<int> lt1 = { 10,2,3,3,4,5,6 };list<int> ::iterator it = lt1.begin();while (it != lt1.end()){cout << *it << " ";it++;}cout << endl;//list不支持sort//sort(lt1.begin(), lt1.end(), greater<int>());//list有自己的排序算法lt1.sort();it = lt1.begin();while (it != lt1.end()){cout << *it << " ";it++;}cout << endl;lt1.sort(greater<int>());it = lt1.begin();while (it != lt1.end()){cout << *it << " ";it++;}cout << endl;//unique 去重//先排序,在去重lt1.unique();for (auto e : lt1){cout << e << " ";  //10 6 5 4 3 2}cout << endl;
}void test_list2()
{//splice:粘接std::list<int> mylist1, mylist2;std::list<int>::iterator it;// set some initial values:for (int i = 1; i <= 4; ++i)mylist1.push_back(i);      // mylist1: 1 2 3 4for (int i = 1; i <= 3; ++i)mylist2.push_back(i * 10);   // mylist2: 10 20 30it = mylist1.begin();++it;                         // points to 2mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4// mylist2 (empty)// "it" still points to 2 (the 5th element
}void test_list3()
{list<int> mylist;for (int i = 1; i <= 4; i++){mylist.push_back(i);  //mylist:   1 2 3 4} //想把3转移到头list<int>::iterator  it = find(mylist.begin(), mylist.end(), 3);//void splice (const_iterator position, list& x, const_iterator i);//把list的第i个位置的数粘接到position位置mylist.splice(mylist.begin(), mylist, it);
}int main()
{//wyj::test_list5();//test_list1();bit::list_test1();return 0;
} 

 /*****************************************************/

 /*****************************************************/





http://www.dtcms.com/a/326348.html

相关文章:

  • 提升行车安全的关键技术:BSD(盲点监测)与DSM(驾驶员监测)是如何工作的?
  • AI(领域)应用落地技术决策指南:从双路径架构到系统性实施
  • Centos 用http ftp搭建本地yum源 保姆级教程
  • 芯片学习 8 :IP集成、cluster、lint
  • 基于MongoDB/HBase的知识共享平台的设计与实现
  • 【09】中兴通讯——中兴 软件工程师 一面,校招,面试问答记录
  • 3DMAX快速散布插件QuickScatter安装使用方法
  • wrap cpp variant as dll for c to use
  • Horse3D引擎研发笔记(四):在QtOpenGL下仿three.js,封装EBO绘制四边形
  • copyleft的遗传特性
  • Python自动化测试实战:reCAPTCHA V3绕过技术深度解析
  • HTTPS应用层协议-中间攻击人
  • Spring AI赋能图像识别:大数据模型驱动下的智能化变革
  • 爬虫的云服务器代理
  • Linux中DNS系统搭建与配置指南(配实验步骤与注释)
  • 车型销售数据爬虫代码详细解释
  • MySQL入门基础
  • 【排序算法】⑥快速排序:Hoare、挖坑法、前后指针法
  • UE 保存游戏
  • 牛客.空调遥控二分查找牛客.kotori和气球(数学问题)力扣.二叉树的最大路径和牛客.主持人调度(二)
  • 数集相等定义凸显解析几何几百年重大错误:将无穷多各异点集误为同一集
  • 免费好用的数字人API
  • 使用dockge 安装 photoprism
  • 最短路问题从入门到负权最短路
  • elasticsearch的高可用
  • 【van-field 会记录上次选择并以悬浮弹窗再次展示】
  • 18.5 BERT评估指标终极指南:HuggingFace实战提升文本分类效果
  • office卸载不干净?Office356卸载不干净,office强力卸载软件下载
  • 爬虫与数据分析结合案例学习总结
  • Note4:Self-Attention