C++ List的模拟实现

Hello!!大家早上中午晚上好,前文我们介绍了list的使用,今天来模拟实现以下吧!

一、初步工作

1.1我们需要一个头文件来声明定义list和一个测试文件来测试List的实现效果
1.2第二步:考虑List 需要用变量、成员函数、以及实现方法

①首先List的底层是一个带头双向循环链表,所以必须要定义Node节点,节点里要有一个prev前驱指针指向前一个节点和一个next后继指针指向下一个节点,Node节点里存放数据data;

②其次链表里要有一个头结点,让头结点指向的第一个节点作为begin(),让头结点成为end(),因为头节点里是不存放数据的正好可以用作链接,图解:

③再者需要定义迭代器,begin()返回指向node1的迭代器,end()返回指向head的迭代器;且迭代器还要实现++、--、解引用、->的重载;

④对于insert、erase,insert返回插入的新节点的迭代器,erase为了防止迭代器失效返回删除的节点的下一个节点的迭代器;

二、开始实现

2.1 链表存放的节点的定义
cpp 复制代码
//节点的定义(定义成模版可以接受各种类型)
template<class T>
struct list_node
{
	list_node<T>* _prev;
	list_node<T>* _next;
	T _data;
	list_node(const T&val=T())
		:_prev(nullptr),_next(nullptr),_data(val)
	{

	}
};
2.2 链表的迭代器的封装
cpp 复制代码
//迭代器
template<class T,class Ref,class Ptr>
struct _list_iterator
{
	typedef _list_iterator<T,Ref,Ptr> Self;
	typedef list_node<T> Node;
	Node* _node;
	_list_iterator()
		:_node(nullptr)
	{}
	_list_iterator(Node* node)
	{
		_node = node;
	}
	_list_iterator(const Self& it)
	{
		_node = it._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)const
	{
		return _node != it._node;
	}
	bool operator==(const Self& it)const
	{
		return _node == it._node;
	}
};
2.3 链表的定义
cpp 复制代码
	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 _head->_next;
		}
		const_iterator begin()const
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator end()const
		{
			return _head;
		}
		void emptyinit()//对空链表的初始化
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}

		_list()
		{
			emptyinit();
		}

      	_list(const _list<T>& lt)// list<int>  list(list2);
    	{
		emptyinit();
		for (auto e:lt)
		{
			push_back(e);
		}
	    }

       	void swap(_list<T>& lt)
	    {
		std::swap(_head, lt._head);
		std::swap(_size, lt._size);
    	}

    	_list<T>& operator=( _list<T> lt)// list2=list3=list4;
	    {
		swap(lt);
		return *this;
    	}

        ~_list()
	   {
		clear();
		delete _head;
		_head = nullptr;
	   }
	   void clear()
	   {
		iterator it = begin();
		while (it != end())
		{
			it=erase(it);
		}
		_size = 0;
	   }
		iterator insert(iterator pos, const T& val = T())
		{
			Node* newnode = new Node(val);
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			prev->_next = newnode;
			newnode->_next = cur;
			cur->_prev = newnode;
			newnode->_prev = prev;
			++_size;
			return newnode;
		}
		iterator erase(iterator pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			--_size;
			return next;
		}
		void push_back(const T& val = T())
		{
			insert(end(), val);
		}
		void pop_back()
		{
			erase(--end());
		}
		void push_front(const T& val = T())
		{
			insert(begin(), val);
		}
		void pop_front()
		{
			erase(begin());
		}
	private:
		Node* _head;
		size_t _size;
	};

三、测试

3.1 尾插尾删
cpp 复制代码
void test2()
{
	ldc::_list<int> list;
	list.push_back(1);
	list.push_back(2);
	list.push_back(3);
	list.push_back(4);
	list.push_back(5);
	list.pop_back();
	list.pop_back();
	for (auto e : list)
	{
		cout << e << " ";
	}
	cout << endl;
}
3.2 头插头删
cpp 复制代码
void test3()
{
	ldc::_list<int> list;
	list.push_front(1);
	list.push_front(2);
	list.push_front(3);
	list.push_front(4);
	for (auto e : list)
	{
		cout << e << " ";
	}
	cout << endl;
	list.pop_front();
	list.pop_front();
	for (auto e : list)
	{
		cout << e << " ";
	}
	cout << endl;

}
3.3 迭代器访问
cpp 复制代码
void test4()
{
	ldc::_list<int> list;
	list.push_back(1);
	list.push_back(2);
	list.push_back(3);
	list.push_back(4);
	ldc::_list<int>::iterator it1 = list.begin();
	while (it1 != list.end())
	{
		*it1 += 1;
		cout << *it1 << " ";
		++it1;
	}
}
3.4 const迭代器访问
cpp 复制代码
void test5()
{
	ldc::_list<int> list;
	list.push_back(1);
	list.push_back(2);
	list.push_back(3);
	list.push_back(4);
	const ldc::_list<int> list2 = list;
	ldc::_list<int>::const_iterator it1 = list2.begin();
	while (it1 != list2.end())
	{
		*it1 += 1;//报错
		cout << *it1 << " ";
		++it1;
	}
}

以上就是list的大致功能的实现,如果对您有帮助记得点赞收藏+关注哦!!谢谢!!!

咱下期见!!!

相关推荐
顾子茵7 分钟前
c++从入门到精通(五)--异常处理,命名空间,多继承与虚继承
开发语言·c++
南部余额17 分钟前
Python OOP核心技巧:如何正确选择实例方法、类方法和静态方法
开发语言·python
supingemail23 分钟前
面试之 Java 新特性 一览表
java·面试·职场和发展
星星点点洲30 分钟前
【Java】应对高并发的思路
java
LDM>W<32 分钟前
黑马点评-用户登录
java·redis
保利九里38 分钟前
数据类型转换
java·开发语言
冲帕Chompa38 分钟前
图论part10 bellman_ford算法
数据结构·算法·图论
Uranus^42 分钟前
使用Spring Boot与Spring Security构建安全的RESTful API
java·spring boot·spring security·jwt·restful api
蚂蚁在飞-43 分钟前
Golang基础知识—cond
开发语言·后端·golang
Aiden Targaryen43 分钟前
Windows/MacOS WebStorm/IDEA 中开发 Uni-App 配置
java·uni-app·webstorm