0
点赞
收藏
分享

微信扫一扫

代码随想录Day29

北冥有一鲲 18小时前 阅读 2

目录

本节目标

 1. list的介绍及使用

1.2 list的使用

2.list的模拟实现 

1.对list进行初步的实现

2.头插和任意位置的插入

3.pos节点的删除,头删,尾删

4.销毁list和析构函数

5.const迭代器

6.拷贝构造和赋值操作

3.完整代码 


本节目标


 1. list的介绍及使用


1.2 list的使用


2.list的模拟实现 


3.完整代码 

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

namespace delia
{
	template<class T>
	struct _list_node
	{
		T _val;
		_list_node<T>* _prev;
		_list_node<T>* _next;

		_list_node(const T& val = T())
			:_val(val)
			, _prev(nullptr)
			, _next(nullptr)
		{};
	};

	template<class T, class Ref>
	struct _list_iterator//使用_list_iterator类来封装node*
	{
		typedef _list_node<T> node;
		typedef _list_iterator<T, Ref> self;

		node* _pnode;

		//构造函数
		_list_iterator(node* pnode)
			:_pnode(pnode)
		{}

		//拷贝构造、赋值运算符重载、析构函数,编译器默认生成即可

		//解引用,返回左值,是拷贝,因此要用引用返回
		Ref operator*()
		{
			return _pnode->_val;
		}

		//!=重载
		bool operator!=(const self& s) const
		{
			return _pnode != s._pnode;
		}

		//==重载
		bool operator==(const self& s) const
		{
			return _pnode == s._pnode;
		}

		//前置++  it.operator(&it)
		self& operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		//后置++ 返回++之前的值  it.operator(&it,0)
		self operator++(int)
		{
			self tmp(*this);
			_pnode = _pnode->_next;
			return tmp;
		}

		//前置--  it.operator(&it)
		self& operator--()
		{
			_pnode = _pnode->prev;
			return *this;
		}

		//后置++ 返回++之前的值  it.operator(&it,0)
		self operator--(int)//临时对象不能用引用返回,所以self没有加&
		{
			self tmp(*this);
			_pnode = _pnode->_prev;
			return tmp;
		}
	};

	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;//重命名const迭代器

		//构造函数
		list()
		{
			_head = new node;//会调_list_node的构造函数
			_head->_next = _head;//整个链表只有头节点,先构造一个没有实际节点的链表
			_head->_prev = _head;//整个链表只有头节点,先构造一个没有实际节点的链表
		}

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

			//将lt的元素全部尾插到新链表
			for (const auto& e : lt)
			{
				push_back(e);
			}
		}

		//赋值重载
		list<T>&operator=(list<T>&lt)
		{
			swap(_head, lt._head);
			return *this;
		}
		template <class T> void swap(T& a, T& b)
		{
			T c(a);
			a = b;
			b = c;
		}
		//析构
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

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

		iterator end()
		{
			return iterator(_head);//尾节点的下一个节点位置即头节点
		}

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

		const_iterator end() const
		{
			return const_iterator(_head);//尾节点的下一个节点位置即头节点
		}

		//插入节点
		void insert(iterator pos, const T& x)
		{
			assert(pos._pnode);
			node* newnode = new node(x);//构造节点

			node* prev = pos._pnode->_prev;

			//插入节点
			newnode->_next = pos._pnode;
			pos._pnode->_prev = newnode;
			prev->_next = newnode;
			newnode->_prev = prev;

		}

		//删除节点
		iterator erase(iterator pos)
		{
			assert(pos._pnode);//判断该位置节点是否存在
			assert(pos != end());//end()是最后一个节点的下一个节点位置,也就是头节点,头节点不能删,需要断言

			node* prev = pos._pnode->_prev;//pos位置节点的前一个节点
			node* next = pos._pnode->_next;//pos位置节点的后一个节点

			//删除节点
			delete pos._pnode;
			prev->_next = next;
			next->_prev = prev;

			return iterator(next);//删除之后pos失效,把下一个位置的迭代器给它
		}

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

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

		//尾插
		void push_back(const T& x)
		{
			insert(end()--, x);
		}

		//头删
		void pop_front()
		{
			erase(begin());
		}

		//尾删
		void pop_back()
		{
			erase(--end());
		}

		//判空
		bool empty()
		{
			return _head->_next == _head;
		}

		//求节点个数
		size_t size()
		{
			iterator it = begin();
			size_t sz = 0;
			while (it != end())//时间复杂度O(N)
			{
				it++;
				sz++;
			}

			return sz;
		}
	private:
		node* _head;
	};

	void PrintList(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << it._pnode->_val << " ";
			it++;
		}
		cout << endl;
	}
}
举报

相关推荐

0 条评论