【C++初阶】第12课—list-4. 代码

时间:2025-03-30 11:45:40
  • 头文件代码
#pragma once

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

namespace TT
{
	template<class T>
	struct list_node
	{
		list_node<T>* prev;
		list_node<T>* next;
		T data;

		//构造初始化
		list_node(const T& x = T())
			:prev(nullptr)
			,next(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* _node)
			:node(_node)
		{}

		//解引用操作符*重载
		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& it)
		{
			return node != it.node;
		}
		//==运算符重载
		bool operator==(const Self& it)
		{
			return node == it.node;
		}
	};

	//const修饰的迭代器
	//template<class T>
	//struct list_const_iterator
	//{
	//	typedef list_node<T> Node;
	//	Node* node;

	//	//迭代器的构造函数
	//	list_const_iterator(Node* _node)
	//		:node(_node)
	//	{}

	//	//解引用操作符*重载
	//	const T& operator*()
	//	{
	//		return node->data;
	//	}

	//	//解引用操作符->重载
	//	const T* operator->()
	//	{
	//		return &node->data;
	//	}

	//	//++运算符重载
	//	list_const_iterator<T>& operator++()
	//	{
	//		node = node->next;
	//		return *this;
	//	}
	//	//!=运算符重载
	//	bool operator!=(const list_const_iterator<T>& it)
	//	{
	//		return node != it.node;
	//	}
	//	//==运算符重载
	//	bool operator==(const list_const_iterator<T>& it)
	//	{
	//		return node == it.node;
	//	}
	//};

	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);
		}

		iterator end()
		{
			return iterator(head);
		}

		const_iterator begin() const 
		{
			return const_iterator(head->next);
		}

		const_iterator end() const
		{
			return const_iterator(head);
		}

		size_t size()
		{
			return _size;
		}

		size_t size() const
		{
			return _size;
		}

		void empty_init()
		{
			head = new Node;
			head->prev = head;
			head->next = head;
			_size = 0;
		}

		//构造
		list()
		{
			empty_init();
		}

		//多参数初始化
		list(initializer_list<T> lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}

		//深拷贝--->lt2(lt1)
		list(const list<T>& lt)
		{
			empty_init();
			for (auto e : lt)
			{
				push_back(e);
			}
		}

		//swap交换
		void swap(const list<T>& lt)
		{
			std::swap(head, lt.head);
			std::swap(_size, lt._size);
		}

		//赋值运算符重载--->lt3 = lt1
		list<T>& operator=(const list<T> lt)
		{
			swap(lt);
			return *this;
		}

		//尾插
		void push_back(const T& x)
		{
			//Node* newnode = new Node(x);
			//Node* tail = head->prev;
			//newnode->prev = tail;
			//tail->next = newnode;
			//newnode->next = head;
			//head->prev = newnode;

			insert(end(), x);
		}
		//头插
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		//头删
		void pop_front()
		{
			erase(begin());
		}
		//尾删
		void pop_back()
		{
			erase(--end());
		}
		//pos位置插入数据
		void insert(iterator pos, const T& x)
		{
			Node* cur = pos.node;
			Node* prev = cur->prev;
			Node* newnode = new Node(x);
			prev->next = newnode;
			newnode->prev = prev;
			newnode->next = cur;
			cur->prev = newnode;
			++_size;
		}
		//erase删除指定位置节点
		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos.node;
			Node* PrevNode = cur->prev;
			Node* NextNode = cur->next;

			PrevNode->next = NextNode;
			NextNode->prev = PrevNode;
			delete cur;
			--_size;
			return iterator(NextNode);
		}

		//析构
		~list()
		{
			clear();
			delete head;
			head = nullptr;
		}

		//clear清除
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
	private:
		Node* head;
		size_t _size;
	};
}
  • 后缀cpp代码
#include "list.h"
#include<algorithm>

struct A
{
	A(int a1 = 1, int a2 = 1)
		:_a1(a1)
		,_a2(a2)
	{ }

	int _a1;
	int _a2;
};

void list_test1()
{
	TT::list<int> lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);

	TT::list<int>::iterator it = lt1.begin();
	while (it != lt1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

void Print(const TT::list<A>& lt)
{
	TT::list<A>::const_iterator it = lt.begin();
	while (it != lt.end())
	{
		//it->_a1 += 1;
		cout << (*it)._a1 << " " << (*it)._a2 << endl;;
		++it;
	}
	cout << endl;
}

void list_test2()
{
	TT::list<A> lt1;
	lt1.push_back({ 1,1 });
	lt1.push_back({ 2,2 });
	lt1.push_back({ 3,3 });
	lt1.push_back({ 4,4 });

	TT::list<A>::iterator it1 = lt1.begin();
	while (it1 != lt1.end())
	{
		//cout << (*it1)._a1 << ":" << (*it1)._a2 << endl;
		it1->_a1 += 1;
		cout << it1->_a1 << ":" << it1->_a2 << endl;
		++it1;
	}
	cout << endl;

	Print(lt1);
}

void list_test3()
{
	TT::list<int> lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);

	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.push_back(78);
	lt1.push_back(99);
	lt1.push_front(100);
	lt1.push_front(300);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.pop_back();
	lt1.pop_front();
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_list4()
{
	TT::list<int> lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);

	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	TT::list<int> lt2(lt1);

	lt1.clear();
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;

	TT::list<int> lt3 = lt2;
	for (auto e : lt3)
	{
		cout << e << " ";
	}
	cout << endl;

	TT::list<int> lt4 = { 10,20,30,40 };
	for (auto e : lt4)
	{
		cout << e << " ";
	}
	cout << endl;
}

int main()
{
	//list_test1();
	//list_test2();
	//list_test3();
	test_list4();
	return 0;
}