模拟实现简单list

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

cpp 复制代码
#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);
	}

}

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

相关推荐
kyle~13 分钟前
计算机系统---USB的四种传输方式
运维·c++·计算机系统
Lhan.zzZ15 分钟前
Qt数据可视化实战:饼图、线图与表格的完整指南
开发语言·qt·信息可视化
Acrelhuang17 分钟前
筑牢用电防线:Acrel-1000 自动化系统赋能 35kV 园区高效供电-安科瑞黄安南
java·大数据·开发语言·人工智能·物联网
小龙报37 分钟前
《算法通关指南数据结构和算法篇(4)--- 队列和queue》
c语言·开发语言·数据结构·c++·创业创新·学习方法·visual studio
打不了嗝 ᥬ᭄44 分钟前
【Linux】网络层协议
linux·网络·c++·网络协议·http
民乐团扒谱机44 分钟前
深入浅出理解克尔效应(Kerr Effect)及 MATLAB 仿真实现
开发语言·matlab·光学·非线性光学·克尔效应·kerr effect
7澄11 小时前
深入解析 LeetCode 数组经典问题:删除每行中的最大值与找出峰值
java·开发语言·算法·leetcode·intellij idea
计算衎1 小时前
.c .o .a .elf .a2l hex map 这些后缀文件的互相之间的联系和作用
开发语言·elf·gcc·c/c++·a2l
ysyxg1 小时前
设计模式-策略模式
java·开发语言
feiyangqingyun1 小时前
Qt/C++编写GB28181服务/前后端分离/定义一套交互协议/视频点播/录像回放和控制/警情通知
c++·qt·交互