C++list的模拟实现(详细代码)

在学习c++中,对于list的练习能帮助我们更好的理解迭代器的原理,list中较为重要的就是对迭代器的理解和实现

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace bai
{
	template<class  T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _val;
		list_node(const T& val = T())//缺省参数,没有时T()会匿名生成一个T类型的
			:_next(nullptr)
			, _prev(nullptr)
			, _val(val)
		{}
	};

	//迭代器
	template<class T,class Ref,class Ptr>
	struct _list_iterator
	{
		typedef list_node<T> Node;
		//typedef list_node<T, Ref,Ptr> self;
		typedef _list_iterator<T, Ref, Ptr> self;
        Node* _node;
		_list_iterator(Node* node)
			:_node(node)
		{
		}
		
		Ref operator* ()//加Ref为了实现const类型
		{
			return _node->_val;
		}
		Ptr operator->()//编译器特殊处理省略->   //Ptr也是为了实现对const的处理
		{
			return &_node->_val;
		}
		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)//后置--加int为了实现函数重载来区分
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const self& it)const//加const是因为传值时具有常性
		{
			return _node != it._node;
		}
		bool operator==(const self& it)const//加const是因为传值时具有常性
		{
			return _node == it._node;
		}
		//这里的迭代器没有写析构函数,析构后该节点就没了无法满足list需求

	};

	template<class T>
	class list//带头双向循环链表
	{
		typedef list_node<T> Node;
	public:
		//利用模板进行解决const的问题
		/*typedef _list_iterator<T,T&,T*> iterator;
		typedef _list_iterator<T,const T&,const T*> cosnt_iterator;*/
		typedef _list_iterator<T, T&, T*> iterator;
		typedef _list_iterator<T, const T&, const T*> const_iterator;
		//const迭代器期望的是内容不能修改,
		//typedef const _list_iterator<T>  iterator;

		iterator begin()
		{
			//return _head->next;
			return iterator(_head->_next);//匿名对象隐式类型转换
		}
		iterator end()
		{
			return _head;
			//return iterator(_head);//匿名对象隐式类型转换
		}
		const_iterator begin()const
		{
			//return _head->next;
			return iterator(_head->_next);//匿名对象隐式类型转换
		}
		const_iterator end()const
		{
			return _head;
			//return iterator(_head);//匿名对象隐式类型转换
		}
		void empty_init()
		{
			_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;
			_size = 0;
		}
		list()
		{
			/*_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;*/
			empty_init();
		}
 
		list(const list<T>& it)//拷贝构造
			//也可以用list(const list& it)    list&和list<T>&都可以用在这种情况
		{
			empty_init();

			for (auto& ch : it)//加引用是为了不同类型
			{
				push_back(ch);
			}
		}
	 
		void swap(list<T>& it)
		{
			std::swap(_head, it._head);
			std::swap(_size, it._size);

		}

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

		}
		void clear()
		{
			iterator it=begin();
			while (it!=end())
			{
				it = erase(it);//删除后it位置发生改变,因此要对it进行赋值来改变  防止迭代器失效
			}
			_size = 0;

		}


		//有了insert和erase函数我们可以复用
		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(x);

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

			cur->_prev = newnode;
			newnode->_next = cur;
			++_size;
			return newnode;//返回迭代器是为了防止迭代器失效,pos位置发生改变

		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* next = cur->_next;
			Node* prev = cur->_prev;
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			--_size;
			return next;
		}
		void push_back(const T& x)
		{
			//Node* tail = _head->_prev;
			//Node* newnode = new Node(x);//调用构造函数

			//tail->_next = newnode;
			//newnode->_prev = tail;

			//newnode->_next = _head;
			//_head->_prev = newnode;
			insert(end(), x);
		}
		void pop_back()
		{
			erase(--end());
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_front()
		{
			erase(begin());
		}
 
		size_t size()
		{
			//这样的效率不高
		/*	size_t sz = 0;
			iterator it = begin();
			while (it != end())
			{
				++sz;
				++it;
			}
			return sz;*/

			//我们定义了一个成员变量,每次添加数据删除数据都会改变
			return _size;

		}
	private:
		Node* _head;
		size_t _size;
	};
}

部分测试:

#include<iostream>
#include<assert.h>
using namespace std;
void test()
{
		bai::list<int> n;
		n.push_back(1);
		n.push_back(2);
		n.push_back(3);
		n.push_back(4);
		n.pop_back();
		n.push_front(3);
		n.pop_front();
		for (auto ch : n)
		{
			cout << ch << " ";
		 
		}
		//bai::list<int>::iterator it = n.begin();//是通过拷贝构造
		/*while (it != n.end())
		{
			cout << *it << " ";
			++it;
		}*/
		cout << endl;
		

}
int main()
{
	test();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大大白的小小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值