C++ list模拟实现

简单了解

在这里插入图片描述

    // 升序 < less
	lt.sort(); //归并算法
	// 降序 > greater
	//greater<int> gt;
	//lt.sort(gt);
	lt.sort(greater<int>());

如果想测两个排序的效率,记得用release,因为debug下测试不准确

void test()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	list<int> lt2;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand();
		lt1.push_back(e);
		lt2.push_back(e);
	}

	int begin1 = clock();

	// 拷贝到vector
	vector<int> v(lt2.begin(), lt2.end());
	// 对vector排序
	std::sort(v.begin(), v.end());

	// 拷贝回lt2
	lt2.assign(v.begin(), v.end());

	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("list copy vector sort copy list sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

在这里插入图片描述
每个节点的结构

template<class T>
	struct list_node
	{
		list_node<T>* next;
		list_node<T>* prev;
		T date;
		list_node(const T&x=T())
			:date(x)
			,next(nullptr)
			,prev(nullptr)
		{}
	};

迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

需要用typename的情况

当我们想用一个函数负责打印容器里的数据:

	 //实例化
	template<typename T>
	//template<class T>
	void print_list(const list<T>& lt)
	{
		// list<T>未实例化的类模板,编译器不能去他里面去找
		// 编译器就无法list<T>::const_iterator是内嵌类型,还是静态成员变量
		// 前面加一个typename就是告诉编译器,这里是一个类型,等list<T>实例化,再去类里面去取
		typename list<T>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

改进:

template<typename Container>
	void print_container(const Container& con)
	{
		typename Container::const_iterator it = con.begin();
		while (it != con.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

list的反向迭代器

在这里插入图片描述
可以给反向迭代器专门给一个类型,和正向迭代器相似
修改数据一般用到 *和-> ,那么加上const就不能修改了
在这里插入图片描述

如果重新再写一份反向的会比较冗余

正向和反向迭代器的区别就是返回值不一样
库里实现的:都是__list_iterator这个类模板,但是传了三个模板参数

在这里插入图片描述

模拟实现list

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

namespace ls
{
	template<class T>
	struct list_node
	{
		list_node<T>* next;
		list_node<T>* prev;
		T date;
		list_node(const T&x=T())
			:date(x)
			,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> self;
		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}
		Ref operator*()
		{
			return _node->date;
		}
		Ptr operator->()
		{
			return &_node->date;
		}
		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
	{
		typedef list_node<T> Node;
	public:
		typedef __list_iterator<T,T&,T*> iterator;
		typedef __list_iterator<T,const T&,const T*> const_iterator;
		list()
			:_head(nullptr)
			,_size(0)
		{
			empty_init();
		}
		list(int n, const T& x = T())
			:_head(nullptr)
			, _size(0)
		{
			empty_init();
			while (n--)
			{
				push_back(x);
			}
		}
		list(iterator first, iterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		list(const list<T>& l)
		{
			empty_init();
			for (auto e : l)
			{
				push_back(e);
			}
		}
		list<int>& operator=(list<T> tmp)
		{
			swap(tmp);
			return *this;
		}
		~list()
		{
			clear();
			delete[] _head;
			_head = nullptr;
		}
		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()const
		{
			return _size;
		}
		bool empty()const
		{
			return _size == 0;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it=erase(it);
			}
		}
		void empty_init()
		{
			_head = new Node;
			_head->next = _head;
			_head->prev = _head;
			_size = 0;
		}
		void push_back(const T& x)
		{
			iterator it = end();
			insert(it,x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);//隐式类型转换
		}
		void pop_back()
		{
			erase((--end()));
		}
		void pop_front()
		{
			iterator it = iterator(begin());
			erase(it);
		}
		iterator insert(iterator pos,const T& x)//pos位置之前插入,返回新节点位置的iterator
		{
			Node* prev = pos._node->prev;
			Node* cur = pos._node;
			Node* newnode = new Node(x);
			prev->next = newnode;
			newnode->prev = prev;
			newnode->next = cur;
			cur->prev = newnode;

			++_size;
			return iterator(newnode);
		}
		iterator erase(const iterator& pos)//返回被删的下一位置的迭代器
		{
			assert(_size > 0);
			Node* prev = pos._node->prev;//
			Node* next = pos._node->next;
			delete[]pos._node;
			prev->next = next;
			next->prev = prev;
			--_size;

			return iterator(next);
		}
		void swap(list<T>& l)
		{
			std::swap(l._head, _head);
			std::swap(l._size, _size);
		}
	private:
		Node* _head=nullptr;
		size_t _size = 0;
	};
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值