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

模拟实现简单list

list在实际中使用也较为广泛,模拟实现其底层可以让我们更加对模版,类和对象等内容更加了解。

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;


namespace cls
{
	template<class T>
	struct list_node
	{
		list_node(const T& x = T())
			:_data(x)
			,_next(nullptr)
			,_prev(nullptr)
		{

		}

		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;
	};

	template<class T,class Ref,class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T,Ref,Ptr> Self;

		list_iterator(Node* x)
			:_node(x)
		{

		}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->data;
		}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator--(int)
		{
			Self temp = *this;
			_node = _node->_prev;

			return temp;
		}

		Self operator++(int)
		{
			Self temp = *this;
			_node = _node->_next;

			return temp;
		}
		   
		bool operator==(const Self& x)  const
		{
			return _node == x._node;
		}

		bool operator!=(const Self& x) const
		{
			return _node != x._node;
		}


		Node* _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;

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

		list()
		{
			empty_init();
		}

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

		void swap(list<T>& x)
		{
			std::swap(_size,x._size);
			std::swap(_head,x._head);
		}

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

		list<T>& operator=(list<T> x)
		{
			swap(x);
			
			return *this;
		}

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


		bool empty() const
		{
			return  _size == 0;
		}

		size_t size() const
		{
			return _size;
		}

		void push_back(const T& x)
		{
			Node* newnode = new Node(x);
			Node* tail = _head->_prev;
			_head->_prev = newnode;
			newnode->_next = _head;
			tail->_next = newnode;
			newnode->_prev = tail;
			++_size;
		}

		iterator begin()
		{
			return _head->_next;
		}

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

		const_iterator end()  const
		{
			return _head;
		}



		iterator end()
		{
			return _head;
		}

		iterator insert(iterator pos,const T& x)
		{
			Node* newnode = new Node(x);
			Node* prev = pos._node->_prev;
			newnode->_next = pos._node;
			newnode->_prev = prev;
			prev->_next = newnode;
			pos._node->_prev = newnode;
			++_size;

			return newnode;
		}

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

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* prev = pos._node->_prev;
			Node* next = pos._node->_next;
			delete pos._node;
			prev->_next = next;
			next->_prev = prev;
			--_size;

			return next;
		}

		void pop_front()
		{
			erase(begin());
		}

		void pop_back()
		{
			erase(--end());
		}

	private:
		Node* _head;
		size_t _size;
	};

	template<class Container>
	void print_container(const Container& con)
	{
		typename Container::const_iterator it = con.begin();
		while (it != con.end())
		{   
			cout << *it << ' ';
			++it;
		}
		cout << endl;
		for (const auto& e : con)
		{
			cout << e << ' ';
		}
		cout << endl;
	}


	void Test()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(10);
		lt.push_back(10);
		lt.push_back(2);
		lt.push_back(3);
		lt.erase(lt.begin());
		print_container(lt);
		lt.insert(lt.begin(),100);
		print_container(lt);
		list<int> lt1 = lt;
		
		print_container(lt1);
	}

}

这是一个对于初学者比较友好的简单版本的模拟实现,希望对大家有所帮助。

相关文章:

  • 一种JSON多态表示法
  • UniApp的神器-开启前端开发的全新篇章
  • 解决ModuleNotFoundError: No module named ‘distutils‘
  • xiaomi pad 6PRO 小米平板6 pro hyperOS降级 澎湃os 降级MIUI 14 教程 免解锁BL 降级,168小时解锁绑定
  • Xcode数据分析与可视化:解锁应用优化的密钥
  • linux操作两个文件,a里的数据删b
  • rust交叉编译
  • 【linux】linux中如何通过systemctl来创建和管理服务
  • 电销机器人引领电销变革
  • servlet的执行顺序
  • 初探 Rust 语言与环境搭建
  • 多功能声学馆的卓越优势:剧院级音效的全新体验—轻空间
  • 【游戏引擎之路】登神长阶(九)——《3D游戏编程大师技巧》:我想成为游戏之神!
  • 【Python学习-UI界面】PyQt5 小部件1-Label
  • WPF-实现多语言的静态(需重启)与动态切换(不用重启)
  • 前端 JavaScript 的 _ 语法是个什么鬼?
  • 自建Gitlab和Gitlab runner并推送镜像到Harbor
  • 学习STM32(6)-- STM32单片机ADCDAC的应用
  • Halcon图像平滑与去噪
  • 关系,条件和逻辑操作符详解
  • 援藏博士张兴堂已任西藏农牧学院党委书记、副校长
  • 我驻苏丹使馆建议在苏中国公民尽快撤离
  • 马上评|比余华与史铁生的友情更动人的是什么
  • 李云泽:支持设立新的金融资产投资公司,今天即将批复一家
  • 怎样正确看待体脂率数据?或许并不需要太“执着”
  • 沪幼升小网上报名明起开始,是否参与民办摇号怎么定?