简易网站只做网站不做app
web/
2025/9/27 5:10:29/
文章来源:
简易网站,只做网站不做app,辽宁大连建设工程信息网,枣庄公司做网站本章主要是讲模拟实现list#xff0c;文章末附上代码。
目录
一、创建思路
二、构造函数 三、迭代器
四、增删
五、代码 一、创建思路
如下方代码#xff0c;链表是由一块一块不连续的空间组成的#xff0c;所以这里写了三个模板#xff0c;一个是节点#xff0c;一…本章主要是讲模拟实现list文章末附上代码。
目录
一、创建思路
二、构造函数 三、迭代器
四、增删
五、代码 一、创建思路
如下方代码链表是由一块一块不连续的空间组成的所以这里写了三个模板一个是节点一个是迭代器分别放在struct创建的类因为这个是可以直接访问从下方代码可以看出我是在list里面定义了一个head这个就是哨兵位头节点然后在list_node里面写的就是节点的初始化需要使用时直接new一个_list_iterator这个就是迭代器写的地方了这里也是直接写了两个一个普通的迭代器一个const的。 namespace ly { templateclass T struct list_node { list_nodeT* _next; list_nodeT* _prev; T _data; list_node(const T x T()) :_next(nullptr) , _prev(nullptr) , _data x {} }; templateclass T,class Ref,class Ptr struct _list_iterator { typedef list_nodeT node; typedef __list_iteratorT, Ref, Ptr self; node* _node; node* _node; _list_iterator(node* n) :_node(n) {} }; templateclass T class list { public: typedef list_nodeT node; typedef _list_iteratorT, T, T* iterator; typedef _list_iteratorT, const T, const T* const_iterator; private: node* _head; }; } 二、构造函数
如下方代码所示就是我写的构造函数因为这个链表是一个双向循环带头链表所以直接new一个node在把哨兵位的next和prev指向自己就创建出了一个链表如下方图片可以看出创造出来了。 list() { _head new node; _head-_next _head; _head-_prev _head; } 三、迭代器
这里是把迭代器能用到的都写了例如解引用就是利用这个节点指针直接访问就可以了但是考虑到了可能访问常量指针所以这里就是利用模板参数进行访问的第二个就是相当于访问数据了因为在流输出的时候正常是访问不到因为迭代器访问的是这个节点的额指针这时重载了一个-就可以正常访问了就是下一个节点的地址也就是这个节点里面存入的next前置和后置在之前文章中都说过这里就不详细介绍了后置就是价格int以作区分--也是类似操作与直接判断节点的地址是否相同就可以了。 Ref operator*() { return _node-_data; } Ptr operator-() { return _node-_data; } 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; } bool operator(const self s) { return _node s._node; } bool operator!(const self s) { return _node ! s._node; } 四、增删
再写数据结构的顺序表的时候就知道了头插尾插头删尾删是可以直接使用inster的所以这里是直接写了inster在进行调用的代码如下测试代码如下结果如图这里是直接调用insert的所以就不测试这个了。 iterator begin() { return iterator(_head-_next); } iterator end() { return iterator(_head); } const_iterator begin() const { return const_iterator(_head-_next); } const_iterator end() const { return const_iterator(_head); } void push_back(const T x) { insert(end(),x); } void push_front(const T x) { insert(begin(), x); } void pop_back() { erase(--end()); } void pop_front() { erase(begin()); } void insert(iterator pos,const T x) { node* cur pos._node; node* prev cur-_prev; node* new_node new node(x); prev-_next new_node; new_node-_prev prev; new_node-_next cur; cur-_prev new_node; } void erase(iterator pos) { assert(pos ! end()); node* prev pos._node-_prev; node* next pos._node-_next; prev-_next next; next-_prev prev; delete pos._node; } void Test1() { listint l1; l1.push_back(1); l1.push_back(2); l1.push_back(3); l1.push_back(4); print(l1); l1.push_front(5); l1.push_front(6); l1.push_front(7); l1.push_front(8); print(l1); l1.pop_back(); l1.pop_back(); print(l1); l1.pop_front(); l1.pop_front(); print(l1); } 五、代码
#pragma once
#include assert.h
namespace ly
{templateclass Tstruct list_node{list_nodeT* _next;list_nodeT* _prev;T _data;list_node(const T x T()):_next(nullptr), _prev(nullptr), _data(x){}};templateclass T, class Ref, class Ptrstruct _list_iterator{typedef list_nodeT node;typedef _list_iteratorT, Ref, Ptr self;node* _node;_list_iterator(node* n):_node(n){}Ref operator*(){return _node-_data;}Ptr operator-(){return _node-_data;}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;}bool operator(const self s){return _node s._node;}bool operator!(const self s){return _node ! s._node;}};templateclass Tclass list{public:typedef list_nodeT node;typedef _list_iteratorT, T, T* iterator;typedef _list_iteratorT, const T, const T* const_iterator;list(){_head new node;_head-_next _head;_head-_prev _head;}iterator begin(){return iterator(_head-_next);}iterator end(){return iterator(_head);}const_iterator begin() const{return const_iterator(_head-_next);}const_iterator end() const{return const_iterator(_head);}void push_back(const T x){insert(end(),x);}void push_front(const T x){insert(begin(), x);}void pop_back(){erase(--end());}void pop_front(){erase(begin());}void insert(iterator pos,const T x){node* cur pos._node;node* prev cur-_prev;node* new_node new node(x);prev-_next new_node;new_node-_prev prev;new_node-_next cur;cur-_prev new_node;}void erase(iterator pos){assert(pos ! end());node* prev pos._node-_prev;node* next pos._node-_next;prev-_next next;next-_prev prev;delete pos._node;}private:node* _head;};void print(listint l){listint::iterator it l.begin();while (it ! l.end()){cout *it ;it;}cout endl;}void Test1(){listint l1;l1.push_back(1);l1.push_back(2);l1.push_back(3);l1.push_back(4);print(l1);l1.push_front(5);l1.push_front(6);l1.push_front(7);l1.push_front(8);print(l1);l1.pop_back();l1.pop_back();print(l1);l1.pop_front();l1.pop_front();print(l1);}
}#define _CRT_SECURE_NO_WARNINGS 1
#include iostreamusing namespace std;#include list.hint main()
{ly::Test1();
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82554.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!