c++的学习之路:16、list(3)

04-11 1068阅读

上章有一些东西当时没学到,这里学到了将在补充,文章末附上代码,思维导图。

目录

一、赋值重载

二、带模板的创建

三、析构函数

四、代码

 五、思维导图


一、赋值重载

这里的赋值重载就是直接利用交换函数进行把传参生成的临时数据和需要进行赋值的交换就可以了,代码与测试如下。

list& operator=(list lt)

        {

            swap(lt);

            return *this;

        }

c++的学习之路:16、list(3)

二、带模板的创建

这里是直接把初始化单独拿出来做一个函数,其他的和上篇文章中写vector差不多,都是利用swap去交换临时生成的参数和需要初始化的链表,代码和测试结果如图。

void empty_init()

        {

            _head = new node;

            _head->_next = _head;

            _head->_prev = _head;

        }

        void swap(list& tmp)

        {

            std::swap(_head, tmp._head);

        }

        list()

        {

            empty_init();

        }

        template

        list(Iterator first, Iterator last)

        {

            empty_init();

            while (first != last)

            {

                push_back(*first);

                ++first;

            }

        }

        list(const list& lt)

        {

            empty_init();

            list tmp(lt.begin(), lt.end());

            swap(tmp);

        }

c++的学习之路:16、list(3)

三、析构函数

这里是写了一个清理的函数,就是利用迭代器和后置++进行erase掉节点,最后再把头节点也就是哨兵位节点删除掉就可以了,代码和测试如下。

~list()

        {

            clear();

            delete _head;

            _head = nullptr;

        }

        void clear()

        {

            iterator it = begin();

            while (it != end())

            {

                erase(it++);

            }

        }

 c++的学习之路:16、list(3)

四、代码

#pragma once
#include 
namespace ly
{
	template
	struct list_node
	{
		list_node* _next;
		list_node* _prev;
		T _data;
		list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};
	template
	struct _list_iterator
	{
		typedef list_node node;
		typedef _list_iterator self;
		node* _node;
		_list_iterator(node* n)
			:_node(n)
		{}
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return& _node->_data;
		}
		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)
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		bool operator==(const self& s)
		{
			return _node == s._node;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
	};
	template
	class list
	{
	public:
		typedef list_node node;
		typedef _list_iterator iterator;
		typedef _list_iterator const_iterator;
		void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		void swap(list& tmp)
		{
			std::swap(_head, tmp._head);
		}
		list()
		{
			empty_init();
		}
		template 
		list(Iterator first, Iterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		list(const list& lt)
		{
			empty_init();
			list tmp(lt.begin(), lt.end());
			swap(tmp);
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);
			}
		}
		list& operator=(list lt)
		{
			swap(lt);
			return *this;
		}
		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 push_back(const T& x)
		{
			insert(end(),x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}
		void insert(iterator pos,const T& x)
		{
			node* cur = pos._node;
			node* prev = cur->_prev;
			node* new_node = new node(x);
			prev->_next = new_node;
			new_node->_prev = prev;
			new_node->_next = cur;
			cur->_prev = new_node;
		}
		void erase(iterator pos)
		{
			assert(pos != end());
			node* prev = pos._node->_prev;
			node* next = pos._node->_next;
			prev->_next = next;
			next->_prev = prev;
			delete pos._node;
		}
	private:
		node* _head;
	};
	void print(list l)
	{
		list::iterator it = l.begin();
		while (it != l.end())
		{
			cout 
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]