list模拟实现

文章目录

1.基本框架

c 复制代码
namespace Apex
{
	//结点类
	template<class T>
	struct list_node
	{
		//成员变量
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

		//成员函数

		//构造函数
		list_node(const T& data = T());
	};

	//迭代器类
	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T, Ref, Ptr> iterator;
		
		//成员变量
		Node* _node;
		//成员函数

		//构造函数
		__list_iterator(Node* node);

		//解引用运算符重载
		Ref operator*();
		//成员访问符重载
		Ptr operator->();
		
		//前置++
		iterator& operator++();
		//后置++
		iterator operator++(int);
		//前置--
		iterator& operator--();
		//后置--
		iterator operator--(int);
		
		//关系运算符
		bool operator==(const iterator& it);
		bool operator!=(const iterator& it);
	};

	//链表类
	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();
		const_iterator begin() const;
		iterator end();
		const_iterator end() const;

		//无参构造函数
		list();
		//有参构造函数(初始化n个结点)
		list(size_t n, const T& val = T());
		//迭代器构造函数
		template<class InputIterator>
		list(InputIterator first, InputIterator last);

		//清空链表数据
		void clear();
		//析构函数
		~list();

		//拷贝构造
		list(const list<T>& lt);
		//赋值重载
		list<T>& operator=(list<T> lt);

		//pos前插入
		void insert(iterator pos, const T& x);
		//删除pos处数据
		iterator erase(iterator pos);

		//尾插
		void push_back(const T& x);
		//头插
		void push_front(const T& x);

		//尾删
		void pop_back();
		//头删
		void pop_front();

	private:
		Node* _head;
	};
}

2.list.h

c 复制代码
#include <iostream>
#include <assert.h>
using namespace std;

namespace apex
{
	//公有类--结点
	template<class T>
	struct list_node 
	{
		T _data;				 
		list_node<T>* _next;       
		list_node<T>* _prev;	      

		list_node(const T& data = T())   
			: _data(data)
			, _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> iterator;    
		
		typedef T value_type;
		typedef Ref reference;
		typedef Ptr pointer;

		//成员属性 _node
		Node* _node;   

		//成员函数 
		
		//构造函数
		__list_iterator(Node* node)
			: _node(node)
		{
		
		}

		//解引用运算符重载
		Ref operator*()   
		{
			return _node->_data;       
		}
		//成员访问符重载
		Ptr operator->()   
		{
			return &_node->_data;
		}

		//前置++
		iterator& operator++()   //__list_iterator<T, Ref, Ptr>& operator++() { } 
		{
			_node = _node->_next;  
			return *this;  
		}
		//后置++
		iterator operator++(int) //__list_iterator<T, Ref, Ptr>& operator++(int) { }
		{
			iterator tmp(*this);  
			_node = _node->_next;
			return tmp;
		}
		//前置--
		iterator& operator--() 
		{
			_node = _node->_prev;
			return *this;
		}
		//后置--
		iterator operator--(int) 
		{
			iterator tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		//关系运算符
		bool operator==(const iterator& it) 
		{
			return _node == it._node;
		}
		bool operator!=(const iterator& it) 
		{
			return _node != it._node; 
		}

	};
	
	//class类--链表
	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);
		}

		//哨兵位初始化
		void empty_init()
		{
			_head = new Node();
			_head->_next = _head;
			_head->_prev = _head;
		}
		//无参构造函数
		list() 
		{
			empty_init();
		}
		//有参构造函数(初始化n个结点)
		list(size_t n, const T& val = T()) 
		{   
			empty_init();

			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		//迭代器构造函数
		template<class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			empty_init();

			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		//清空链表数据
		void clear()
		{
			/*
			iterator it = begin();
			while (it != end())
			{
				iterator del = it++;
				delete del._node;
			}
			//更新哨兵位
			_head->_next = _head;
			_head->_prev = _head;
			*/

			iterator it = begin();
			while (it != end())
			{
				//it = erase(it);
				erase(it++);
			}
		}
		//析构函数
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		//拷贝构造
		/*
		list(const list<T>& lt)
		{
		    empty_init();

			for (auto e : lt)
			{
				push_back(e);
			}
		}*/
		//拷贝构造plus
		void swap(list<T>& x)
		{
			std::swap(_head, x._head);
		}
		list(const list<T>& lt)
		{
			empty_init();

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

		//赋值
		/*
		list<T>& operator=(list<T> lt) 
		{
			if (this != &lt)
			{
				clear();           //this->clear();
				for (auto e : lt) 
				{
					push_back(e);  //this->push_back(e);
				}
			}
			return *this;
		}
		*/
        //赋值plus
		list<T>& operator=(list<T> lt) 
		{
			swap(lt);
			return *this;
		}

		//pos前插入
		void insert(iterator pos, const T& x)
		{
			//prv new cur/pos next
			Node* cur = pos._node;
			Node* prv = cur->_prev;

			Node* newnode = new Node(x);
			//prv连接new
			prv->_next = newnode;
			newnode->_prev = prv;
			//new连接cur
			newnode->_next = cur;
			cur->_prev = newnode;
		}

		//删除pos处数据
		iterator erase(iterator pos)
		{
			assert(pos != end());
			//prv cur/pos next
			Node* cur = pos._node;
			Node* prv = cur->_prev;
			Node* next = cur->_next;

			delete cur;
			//prv连接next
			prv->_next = next;
			next->_prev = prv;

			return iterator(next);
		}

		// 尾插
		void push_back(const T& x)
		{
			/*
			//创建新结点
			Node* newnode = new Node(x);
			//定位尾结点
			Node* tail = _head->_prev;
			//tail连接new
			tail->_next = newnode;
			newnode->_prev = tail;
			//new连接head
			newnode->_next = _head;
			_head->_prev = newnode;
			*/

			insert(end(), x);
		}

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

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

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

	private:
		Node* _head;
	};
///
	
	//打印链表(使用const迭代器)
	void print_list(const list<int>& lt) 
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end()) 
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	// 无参构造  尾插  迭代器 
	void test_list1() 
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

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

	//打印函数
	void test_list2() 
	{
		list<int> lt;
		lt.push_back(2);
		lt.push_back(4);
		lt.push_back(6);
		lt.push_back(8);

		print_list(lt);
	}
    
	//创建日期类 测试成员访问运算符
	struct Date 
	{
		int _year;
		int _month;
		int _day;

		Date(int year = 1, int month = 1, int day = 1)
			: _year(year)
			, _month(month)
			, _day(day)
		{

		}
	};
	void test_list3() 
	{
		list<Date> lt;
		lt.push_back(Date(2023, 7, 21));
		lt.push_back(Date(2023, 7, 22));
		lt.push_back(Date(2023, 7, 23));

		list<Date>::iterator it = lt.begin();
		while (it != lt.end()) 
		{
			cout << it->_year << "-" << it->_month << "-" << it->_day << endl;
			it++;
		}
		cout << endl;
	}

	//拷贝构造函数
	void test_list4()
	{
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);

		list<int> lt2(lt1);
		for (auto e : lt2) 
			cout << e << " ";
	}

	//清空函数
	void test_list5() 
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);

		print_list(lt);
		lt.clear();
		print_list(lt);
	}
}

3.test.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include "list.h"
int main(void)
{
	apex::test_list1();

	return 0;
}
相关推荐
ZZZ_O^O22 分钟前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
代码雕刻家1 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
Kalika0-03 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
代码雕刻家3 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
小字节,大梦想4 小时前
【C++】二叉搜索树
数据结构·c++
我是哈哈hh5 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
丶Darling.5 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5206 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
Indigo_code6 小时前
【数据结构】【链表代码】合并有序链表
数据结构·windows·链表
jiyisuifeng19916 小时前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法