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

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

目录

一、赋值重载

二、带模板的创建

三、析构函数

四、代码

五、思维导图


一、赋值重载

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

list<T>& operator=(list<T> lt)

{

swap(lt);

return *this;

}

二、带模板的创建

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

void empty_init()

{

_head = new node;

_head->_next = _head;

_head->_prev = _head;

}

void swap(list<T>& tmp)

{

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

}

list()

{

empty_init();

}

template <class Iterator>

list(Iterator first, Iterator last)

{

empty_init();

while (first != last)

{

push_back(*first);

++first;

}

}

list(const list<T>& lt)

{

empty_init();

list<T> tmp(lt.begin(), lt.end());

swap(tmp);

}

三、析构函数

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

~list()

{

clear();

delete _head;

_head = nullptr;

}

void clear()

{

iterator it = begin();

while (it != end())

{

erase(it++);

}

}

四、代码

复制代码
#pragma once
#include <assert.h>
namespace ly
{
	template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

		list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};

	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* 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 T>
	class list
	{
	public:
		typedef list_node<T> node;
		typedef _list_iterator<T, T&, T*> iterator;
		typedef _list_iterator<T, const T&, const T*> const_iterator;

		void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}

		list()
		{
			empty_init();
		}

		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		list(const list<T>& lt)
		{
			empty_init();

			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

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

		list<T>& operator=(list<T> 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<int> l)
	{
		list<int>::iterator it = l.begin();
		while (it != l.end())
		{
			cout << *it << ' ';
			it++;
		}
		cout << endl;
	}

	void Test1()
	{
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		print(l1);
		l1.push_front(5);
		l1.push_front(6);
		l1.push_front(7);
		l1.push_front(8);
		print(l1);
		l1.pop_back();
		l1.pop_back();
		print(l1);
		l1.pop_front();
		l1.pop_front();
		print(l1);
	}

	void Test2()
	{
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		print(l1);
		list<int> l2(l1);
		print(l2);
		list<int> l3(l1.begin(), l1.end());
		print(l3);
	}

	void Test3()
	{
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		list<int> l2;
		l2.push_back(10);
		l2.push_back(20);
		l2.push_back(30);
		l2.push_back(40);
		print(l1);
		print(l2);
		l1.swap(l2);
		print(l1);
		print(l2);
	}
}

五、思维导图

相关推荐
行者965 分钟前
Flutter跨平台开发在OpenHarmony上的评分组件实现与优化
开发语言·flutter·harmonyos·鸿蒙
阿蒙Amon16 分钟前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
SmartRadio33 分钟前
ESP32添加修改蓝牙名称和获取蓝牙连接状态的AT命令-完整UART BLE服务功能后的完整`main.c`代码
c语言·开发语言·c++·esp32·ble
且去填词1 小时前
Go 语言的“反叛”——为什么少即是多?
开发语言·后端·面试·go
知乎的哥廷根数学学派1 小时前
基于生成对抗U-Net混合架构的隧道衬砌缺陷地质雷达数据智能反演与成像方法(以模拟信号为例,Pytorch)
开发语言·人工智能·pytorch·python·深度学习·机器学习
yeziyfx2 小时前
kotlin中 ?:的用法
android·开发语言·kotlin
charlie1145141912 小时前
嵌入式的现代C++教程——constexpr与设计技巧
开发语言·c++·笔记·单片机·学习·算法·嵌入式
好奇龙猫2 小时前
【AI学习-comfyUI学习-三十二节-FLXU原生态反推+controlnet depth(UNion)工作流-各个部分学习】
人工智能·学习
古城小栈2 小时前
Rust 网络请求库:reqwest
开发语言·网络·rust
hqwest2 小时前
码上通QT实战12--监控页面04-绘制6个灯珠及开关
开发语言·qt·qpainter·qt事件·stackedwidget