STL中List的模拟实现

本文介绍了C++中如何使用模板类实现一个双向链表,并且详细展示了迭代器的定义和实现,包括解引用、不等运算符和自增操作。通过迭代器的重载操作符,使得链表的迭代器可以像指针一样方便地使用。此外,还提供了链表的插入、删除、复制构造函数和赋值操作等方法。
摘要由CSDN通过智能技术生成

在C++中list底层是由双向队列实现的,其中它的迭代器为了实现出和vector容器的迭代器一样的使用效果(vector的空间是连续的,而list的空间是不连续的),因此对list的迭代器进行自定义了一些方法,其中如指针的解引用,迭代器的类中必须重载operator*()、指针的++自定义的是operator++() 返回一个指向下一个节点的指针等等这些对指针的函数重载,实现了和vector容器的迭代器一样的使用方法,list具体实现如下所示:

	
#pragma once
#include<assert.h>
#include<iostream>
using namespace std;

namespace sdk
{
	template<class T>
	struct _list_node
	{
		T _val;
		_list_node<T>* _prev;
		_list_node<T>* _next;

		_list_node(const T &val=T()):_val(val),_prev(nullptr),_next(nullptr)
		{
		}
	};
	
	//template<class T>
	template<class T, class Ref, class Ptr>
	struct _list_iterator
	{
		typedef _list_node<T> node;
		typedef _list_iterator<T, Ref, Ptr> self;

		node* _pnode;

		_list_iterator(node* pnode):_pnode(pnode)
		{}

		Ref& operator*()
		{
			return _pnode->_val;
		}

		bool operator!=( const self& s)const
		{
			return _pnode != s._pnode;
		}
	
		self& operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}
	};
	
	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);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

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

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

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


		List(const List<T>& lt)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;

			for (auto &e : lt)
			{
				push_back(e);
			}
		}

		void push_back(const T& x)
		{
			node* newnode = new node(x);

			node* tail = _head->_prev;

			tail->_next = newnode;
			newnode->_prev = tail;

			newnode->_next = _head;
			_head->_prev = newnode;
		}

		List<T>& operator=(List<T> lt)
		{
			swap(_head, lt._head);
			return *this;
		}
		void insert(iterator pos, const T&x)
		{
			assert(pos._pnode);

			node* cur = pos._pnode;
			node* prev = cur->_prev;
			node* newnode = new node(x);

			prev->_next = newnode;
			newnode->_prev = prev;

			newnode->_next = cur;
			cur->_prev = newnode;
			
		}

		iterator erase(iterator pos)
		{
			assert(pos._pnode);
			assert(pos != end());

			node* cur = pos._pnode;
			node* prev = cur->_prev;
			node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;

			delete pos._pnode;
			
			return iterator(next);
		}
		~List()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			iterator it = _head->_next;
			while (it != _head)
			{
				it = erase(it);
			}


		}
		
	private:
		node* _head;
	};
	void PrintList(const List<int>& lt)
	{
		List<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			//*it += 1; // ?
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
	void Test1()
	{
		sdk:: List<int> l1;
		l1.push_back(5);
		l1.push_back(4);
		l1.push_back(3);
		l1.push_back(2);
		l1.push_back(1);

		List<int>::iterator it = l1.begin();
		//++it;
		++it;

		//l1.insert(it, -1);
		
		sdk::List<int> l2;
		l2.push_back(-1);
		l2.push_back(-2);
		l2.push_back(-3);
		l1.erase(it);
		List<int>copy(l1);

		PrintList(l1);
		PrintList(copy);
		copy = l2;
		PrintList(copy);
		
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值