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

C++之LIST模拟实现(代码纯享版)

目录

文章目录

前言

一、代码

总结



前言

本文主要展示了模拟List的代码实现

一、代码

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace zlh
{
	template<class T>
	struct list_node
	{
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;
		list_node(const T& data=T())
			:_data(data)
			,_next(nullptr)
			,_prev(nullptr)
		{
		}
	};
	template<class T,class ref,class ptr>
	struct list_iterator
	{
		typedef list_node<T> node;
		typedef list_iterator<T,ref,ptr> self;
		node* _node;
		list_iterator(node* node)
			:_node(node)
		{
		}
		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)const
		{
			return _node != s._node;
		}

		bool operator==(const self& s)const
		{
			return _node == s._node;
		}

	};
	
	template<class T>
	class list
	{
		typedef list_node<T> node;
	public:
		typedef list_iterator<T,T&,T*> iterator;
		typedef list_iterator<T, const T&,const T*> const_iterator;
		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin() const
		{
			return _head->_next;
		}
		const_iterator end() const
		{
			return _head;
		}
		void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list()
		{
			empty_init();
		}
		list(initializer_list<T> li)
		{
			empty_init();
			for (auto e : li)
			{
				push_back(e);
			}
		}
		list(const list<T>& x)
		{
			empty_init();
			for (auto& e : x)
			{
				push_back(e);//插入e不是x
			}
		}
		
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			auto 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->_next = _head;
			//newnode->_prev = tail;
			//_head->_prev = newnode;
			//++_size;		
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}
		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}
	
		size_t size() const
		{
			return _size;
		}
		bool empty()const
		{
			return _size == 0;
		}
		iterator insert(iterator pos, const T& x)
		{
			node* cur = pos._node;
			node* prev = cur->_prev;

			node* newnode = new node(x);

			// prev newnode cur
			newnode->_next = cur;
			cur->_prev = newnode;
			newnode->_prev = prev;
			prev->_next = newnode;

			++_size;
			return newnode;
		}
		iterator erase(iterator pos)
		{
			assert(pos!=end());
			node* next = pos._node->_next;
			node* prev = pos._node->_prev;
			prev->_next = next;
			next->_prev = prev;
			delete pos._node;
			--_size;
			return next;
		}


	private:
		node* _head;
		size_t _size;
	};
	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);
		list<int>::iterator is= l1.begin();
		is=l1.insert(is,20);
		//l1.erase(is);
		while (is != l1.end())
		{
			cout << *is << " ";
			++is;
		}
		cout << endl;
		//for (auto e : l1)
		//{
		//	cout << e << " ";
		//}
		list<int>l2(l1);
		list<int>::iterator it = l2.begin();
		for (auto e : l2)
		{
			cout << e << " ";
		}
	}
}


总结

通过模拟实现List类,可以加深我们对list的印象,了解它的底层逻辑,可以使我们更好地去使用它。


http://www.kler.cn/news/342399.html

相关文章:

  • 使用pycharm 开发streamlit的项目,怎么启动项目,进行debug调试
  • 磁盘I/O测试工具-FIO
  • 小猿口算脚本
  • 前端 NPM
  • MedMamba代码解释及用于糖尿病视网膜病变分类
  • 【Flutter】如何生成和修改 Flutter 应用图标
  • 第二阶段:mysql(学完就隐藏版)
  • Linux 查看当前正在使用的 Bash 版本
  • uniapp打包安卓apk步骤
  • MySQL(B站CodeWithMosh)——2024.10.8(11)
  • 【element-tiptap】如何引进系统中的字体?
  • FreeRTOS和Systemview联合调试——M0内核
  • 使用Pytorch+Numpy+Matplotlib实现手写字体分类和图像显示
  • 【分布式微服务云原生】战胜Redis脑裂:深入解析与解决方案
  • Python中的Socket魔法:如何利用socket模块构建强大的网络通信
  • “国货户外TOP1”凯乐石签约实在智能,RPA助力全域电商运营自动化提效
  • 企业如何借力AI,提升人力资源管理的效率完成组织提效变革
  • processing像素画教程
  • 高防服务器为何有时难以防御CC攻击及其对策
  • 手机控车系统是一种高科技的汽车智能控制系统?