C++ list 模拟实现

#pragma once

namespace dk
{
/节点类
	template <class T>
	struct __list_node//struct默认访问权限是共有的
	{
		T _data;
		__list_node<T>* _next;
		__list_node<T>* _prev;

		__list_node(const T& x = T())
			:_data(x)
			, _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)
		{}

		//拷贝构造 赋值重载 析构都不用实现,默认就可以
		bool operator!=(const self& s) const
		{
			return _node != s._node;
		}

		bool operator== (const self& s) const
		{
			return !(*this != s);
		}
		//operator->
		Ptr operator->() const
		{
			return &_node->_data;
		}
		Ref operator*() const
		{
			return _node->_data;
		}
		//it++  it.operator(&it, 0)
		self operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		//++it  it.operator(&it)
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		//it--  
		self operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;

			return tmp;
		}
		//--it
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
	};
///list类
	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 iterator(_head->_next);
			//return _head->_next;
		}

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

		const_iterator end()const 
		{
			return _head;
		}
		list()//构造函数 构造头结点
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		template<class InputIterator>// 重新声明迭代器,迭代器区间[first,last)可以是任意容器的迭代器
		list(InputIterator first, InputIterator last)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;

			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		list(const list<T>& lt)//拷贝构造
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;

			list<T> tmp(lt.begin(), lt.end());
			std::swap(_head, tmp._head);
		}
		//lt1 = lt2
		list<T>& operator=(list<T> lt)//赋值运算符重载
		{
			std::swap(_head, lt._head);
			return *this;
		}

		//lt2(lt1)拷贝构造传统写法
		list(const list<T>& lt)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;

			const_iterator it = lt.begin();
			while (it != lt.end())
			{
				push_back(*it);
				++it;
			}
		}
		~list()//析构函数
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				//it = erase(it);
				erase(it++);
			}
		}
		void push_back(const T& x)
		{
			//node* newnode = new node(x);
			//node* tail = _head->_prev;
			_head ... tail newnode
			//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());//end()是尾的后一个
		}
		void pop_front()//前删
		{
			erase(begin());
		}
		iterator insert(iterator pos, const T& x)//任意位置插入
		{
			node* newnode = new node(x);
			node* cur = pos._node;
			node* prev = cur->_prev;
			//prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;

			return iterator(newnode); 
		}
		iterator erase(iterator pos)//删除
		{
			assert(pos != end());//end()是尾的后一个

			node* cur = pos._node;
			node* prev = cur->_prev;
			node* next = cur->_next;

			//连接
			prev->_next = cur;
			cur->_prev = prev;

			delete cur;
			return iterator(next);
		}
	private:
		//双向带头循环
		node* _head;
	};
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值