c++中list的简单实现

news/2024/5/20 0:31:39 标签: c++, list, windows, 数据结构, 推荐算法, c语言, 链表

文章目录

  • list
    • 介绍
    • 节点类(listNode)
    • __list__iterator(迭代器类)
      • operator->
    • list的成员函数
      • empty_init() 初始化节点
      • list(list<T>& lt) 拷贝构造
      • clear() 清除链表
      • ~list() 析构
      • insert() 插入
      • erase() 删除
      • push_back() 尾插
      • push_front() 头插
      • pop_back() 尾删
      • pop_front() 头删
      • begin() 头节点
      • end() 尾节点
  • 总结

list_2">list

介绍

list:
数据结构中的链表,存储方式是在内存中每一个节点取一段空间用特定的方式链接起来,这样子就不会有浪费的空间
我们用的是带头循环双向链表

listNode_7">节点类(listNode)

因为一个节点中要包含其他信息所以单独弄成一个类

template<class T>
//链表节点类
struct listNode
{
	listNode<T>* _next;//指向下一个节点
	listNode<T>* _prev;//指向上一个节点
	T date;//内容
	
	listNode(const T& x = T())
		:_next(nullptr)
		,_prev(nullptr)
		,date(x)
	{}
};

list__iterator_26">__list__iterator(迭代器类)

为什么要有迭代器类呢?
因为我们要封装一下这个迭代器,让迭代器该有的操作在list也可以用出来。
如果在list内部弄迭代器会很不好弄。

//正向迭代器类
//Ref 来区别const和普通
template<class T,class Ref,class Ptr>
struct __list__iterator
{
	typedef listNode<T> Node;//减少代码
	typedef __list__iterator<T,Ref> self;//来控制他的类别
	Node* _node;//节点
	__list__iterator(Node* node)
		:_node(node)
	{}
	//++it
	self& operator++()
	{
		_node = _node->_next;
		return *this;
	}
	//it++
	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;
	}
	Ref operator*()
	{
		return _node->date;
	}
	bool operator!=(const self& s)
	{
		return _node != s._node;
	}
	bool operator==(const self& s)
	{
		return _node == s._node;
	}
	Ptr operator->()
	{
		return &_node->date;
	}
};

operator->

因为这个比较特殊很难看懂所以我们单独解释
他的本质是->->,代码解释更好看懂
因为之前的设计者觉得不好看,所以做的特殊处理

struct MyStruct
{
	int _a1;
	int _a2;
	MyStruct(int _a1 = 1, int _a2 = 1)
		:_a1(_a1)
		,_a2(_a2)
	{

	}
};
void test2()
{
	list<MyStruct> s;
	s.push_back(MyStruct());
	s.push_back(MyStruct());
	s.push_back(MyStruct());//插入的是一个类
	list<MyStruct>::iterator lt = s.begin();
	while (lt != s.end())
	{
		cout << lt->_a1<<":"<< lt->_a2 << " ";//读这个类里面的内容
		//lt->_a1 的本质是 lt.operator->()->_a1; 特殊处理
		++lt;
	}
}

list_118">list的成员函数

因为我们偷点懒所以把一些东西typedef一下

	typedef listNode<T> Node;
	typedef __list__iterator<T,T&,T*> iterator;
	typedef __list__iterator<T,const T&,const T*> const_iterator;
	typedef Reverselterator<T, T&, T*> reverse_iterator;

empty_init() 初始化节点

因为我们要多次用到所以单独写一个出来方便
同时可以用list()套一下他

void empty_init()//初始化节点
{
	_head = new Node;
	_head->_next = _head;
	_head->_prev = _head;
}

listlistT_lt__139">list(list& lt) 拷贝构造

简单写法
引用的作用是深拷贝,不要弄成浅拷贝了

list(list<T>& lt)
{
	empty_init();
	for (const auto& ch : lt)
	{
		push_back(ch);
	}
}

clear() 清除链表

直接把链表清空

void clear()
{
	iterator it = begin();
	while (it != end())
	{
		it = erase(it);
	}
}

list__165">~list() 析构

清除之后直接把哨兵位删掉就可以了

~list()
{
	clear();
	delete _head;
}

insert() 插入

在某个节点之前插入

iterator insert(iterator pos, const T& x)//在摸个节点之前插入
{
	Node* cur = pos._node;//插入的位置
	Node* prev = cur->_prev;//插入的下一个位置
	Node* newnode = new Node(x);//构成节点
	prev->_next = newnode;
	newnode->_prev = prev;
	newnode->_next = cur;
	cur->_prev = newnode;
	return newnode;
}

erase() 删除

删除某个节点

iterator erase(iterator pos)
{
	assert(pos !=end());//判断他不是哨兵位
	Node* cur = pos._node;
	Node* prev = cur->_prev;
	Node* next = cur->_next;
	prev->_next = next;
	next->_prev = prev;

	delete cur;
	return next;
}

push_back() 尾插

注释这段因为和insert中基本上没区别,所以简单化了

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);
}

push_front() 头插

void push_front(const T& x)//头插
{
	insert(begin(), x);
}

pop_back() 尾删

void pop_back()//尾删
{
	erase(--end());
}

pop_front() 头删

void pop_front()//头删
{
	erase(begin());
}

begin() 头节点

这里因为是要头节点,所以我们直接把begin设置为第一个节点(有用的节点)

const_iterator begin() const
{
	return _head->_next;
}

end() 尾节点

因为end是尾 所以我们把哨兵位当尾,这样子就可以更好的读

const_iterator end() const
{
	return _head;
}

总结

可以先尝试一下 自己实现
代码总体加我自己的注释给在这里
实现完可以自己对比一下

template<class T>
//链表节点类
struct listNode
{
	listNode<T>* _next;
	listNode<T>* _prev;
	T date;
	
	listNode(const T& x = T())
		:_next(nullptr)
		,_prev(nullptr)
		,date(x)
	{}
};
	//正向迭代器类
	//Ref 来区别const和普通
	template<class T,class Ref,class Ptr>
	struct __list__iterator
	{
		typedef listNode<T> Node;
		typedef __list__iterator<T,Ref> self;
		Node* _node;
		__list__iterator(Node* node)
			:_node(node)
		{}
		//++it
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		//it++
		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;
		}
		Ref operator*()
		{
			return _node->date;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
		bool operator==(const self& s)
		{
			return _node == s._node;
		}
		Ptr operator->()
		{
			return &_node->date;
		}
	};
template<class T>
class list
{
	typedef listNode<T> Node;
public:
	typedef __list__iterator<T,T&,T*> iterator;
	typedef __list__iterator<T,const T&,const T*> const_iterator;
	typedef Reverselterator<T, T&, T*> reverse_iterator;
	const_iterator begin() const
	{
		return _head->_next;
	}
	const_iterator end() const
	{
		return _head;
	}
	iterator begin()
	{
		//return iterator(_head->_next);
		return _head->_next;
	}
	iterator end()
	{
		//return iterator(_head);
		return _head;
	}
	iterator rbegin()
	{
		return end();
	}
	iterator rend()
	{
		return begin();
	}
	void empty_init()//初始化节点
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
	}
	list()
	{
		empty_init();
	}
	list(list<T>& lt)
	{
		empty_init();
		for (const auto& ch : lt)
		{
			push_back(ch);
		}
	}
	~list()
	{
		clear();
		delete _head;
	}
	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}
	void swap(list<T>& lt)
	{
		std::swap(_head, lt._head);
	}
	list<T>& operator=(list<T> lt)
	{
		swap(lt);
		return *this;
	}
	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 push_front(const T& x)//头插
	{
		insert(begin(), x);
	}
	void pop_back()//尾删
	{
		erase(--end());
	}
	void pop_front()//头删
	{
		erase(begin());
	}
	iterator insert(iterator pos, const T& x)//在摸个节点之前插入
	{
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* newnode = new Node(x);
		prev->_next = newnode;
		newnode->_prev = prev;
		newnode->_next = cur;
		cur->_prev = newnode;
		return newnode;
	}
	iterator erase(iterator pos)
	{
		assert(pos !=end());
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* next = cur->_next;
		prev->_next = next;
		next->_prev = prev;

		delete cur;
		return next;
	}
private:
	Node* _head;//哨兵位
};

http://www.niftyadmin.cn/n/5411606.html

相关文章

JVM入门篇(面试前速补)

近期看看JVM&#xff0c;看了狂神说入门教学&#xff0c;总结下给大家。 文章目录 1、JVM的位置2、JVM的结构体系3、类加载器及双亲委派机制3.1、类加载器作用3.2、类加载器类型3.3、双亲委派机制 * 4、沙箱安全机制5、Native、方法区5.1、Native&#xff08;本地方法栈引用&a…

testvue-page

1403.vue <template><div class"error-page"><div class"error-code">4<span>0</span>3</div><div class"error-desc">啊哦~ 你没有权限访问该页面哦</div><div class"error-handle&q…

java设计模式课后作业(待批改)

此文章仅记录学习&#xff0c;欢迎各位大佬探讨 实验&#xff08;一&#xff09; 面向对象设计 实验目的 ①使用类来封装对象的属性和功能&#xff1b; ②掌握类变量与实例变量&#xff0c;以及类方法与实例方法的区别&#xff1b; 知识回顾 详情见OOP课件 实验内容…

UDP通信发送和接收 || UDP实现全双工通信

recvfrom ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); 功能: 从套接字中接收数据 参数: sockfd:套接字文件描述符 buf:存放数据空间首地址 …

x86 Ubuntu上编译eudev给龙芯loongarch64架构主机使用

1、下载eudev库eudev-master.zip&#xff0c;链接&#xff1a;eudev库官方地址 2、下载龙芯的交叉编译工具&#xff1a;loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.2.tar.xz&#xff0c;链接&#xff1a;龙芯交叉编译官方地址 3、交叉编译器环境搭建 (1)、…

Ubuntu 20.04 ROS1 与 ROS2 通讯

激光雷达和3d视觉传感器驱动很多都是基于ros1开发的&#xff0c;由于自己项目在ros2环境开发&#xff0c;需要获取从驱动出来的点云数据流&#xff0c;所以尝试订阅ros1出来的点云topic话题&#xff0c;固需要ros1与ros2之间建立通讯连接。 项目环境&#xff1a; Legion-Y7000…

安卓部分手机使用webview加载链接后白屏(Android低版本会出现的问题)

前言 大爷&#xff1a;小伙我这手机怎么打开你们呢这个是白屏什么都不显示。 大娘&#xff1a;小伙我这也是打开你们呢这功能&#xff0c;就是一个白屏什么也没有&#xff0c;你们呢的应用不会有病毒吧。 小伙&#xff1a;我的手机也正常&#xff1b; 同事&#xff1a;我的也正…

[云原生] K8s之pod控制器详解

Pod 是 Kubernetes 集群中能够被创建和管理的最小部署单元。所以需要有工具去操作和管理它们的生命周期,这里就需要用到控制器了。 Pod 控制器由 master 的 kube-controller-manager 组件提供&#xff0c;常见的此类控制器有 Replication Controller、ReplicaSet、Deployment、…