list的模拟实现

list类学习的主要是一个迭代器的封装。

因为list和前面vector,string的物理空间状况不同。
vector和string的物理空间是连续的。迭代器可以直接采用原生指针的解引用,++,- -等
而list的空间分布不是连续的,因此直接++,- -,是访问不到实际的下一个结点,并且节点是一个struct,直接解引用也不是一个value。

因此针对这种情况,我们将迭代器封装成一个类。对类进行运算符重载,仿佛迭代器可以像原生指针一样使用。这就是迭代器的封装性。

迭代器的具体考虑

  • 迭代器遍历使用!=end(),既可以满足之前的顺序表,也可以满足现在的链表以及之后的stl
  • 对于迭代器采用的拷贝模式,因为想要指向空间即可,采用默认的浅拷贝。同时不析构,因为析构应是归list管的而不是迭代器管的。(不然每次开那么多迭代器数据就没了)
  • 同时还要考虑对于const容器对象的迭代器const_iterator,学习通过模板参数来避免写一个仅仅是为了迭代器是否以const形式返回copy了一份几乎完全一致的代码。具体操作就是给迭代器类三个模板参数,根据list的选用迭代器来返回具体的是const reference 还是直接reference或者指针。
namespace YCB {

	//结点的定义
	template<typename T>
	struct _list_node {
		T _val;
		_list_node* _prev;
		_list_node* _next;
		
		_list_node( T val=T() )
			:_val(val)
			,_prev(nullptr)
			,_next(nullptr)
		{

		}
	};

	//迭代器(采用默认的浅拷贝)
	template<typename T,typename ref,typename ptr>
	struct _list_iterator {
		typedef _list_node<T> node;
		typedef _list_iterator<T,ref,ptr> self;

		node* _pnode;//迭代器当前指向的结点

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

		bool operator==(const self& iter) {
			return _pnode == iter._pnode;
		}
		
		bool operator!=(const self& iter) {
			return _pnode != iter._pnode;
		}
		
		ref operator*() {
			return _pnode->_val;
		}

		ptr operator->() {
			return &(_pnode->_val);
		}
		//iterator++
		self operator++(int) {
			self tmp = *this;
			_pnode = _pnode->_next;
			return tmp;
		}
		//++iterator
		self& operator++() {
			_pnode = _pnode->_next;
			return *this;
		}
		//iterator--
		self operator--(int) {
			self tmp = *this;
			_pnode = _pnode->_prev;
			return tmp;
		}
		//--iterator
		self& operator--() {
			_pnode = _pnode->_prev;
			return *this;
		}
	};
	
	template<typename T>
	class list {
	public:
		typedef _list_node<T> node;
		typedef _list_iterator<T, T&, T*> iterator;
		typedef _list_iterator<T, const T&, const T*> const_iterator;
		
		iterator begin() const {
			return iterator(_phead->_next);
		}
		iterator end() const {
			return iterator(_phead);
		}

		const_iterator cbegin() const  {
			return const_iterator(_phead->_next);
		}

		const_iterator cend() const {
			return const_iterator(_phead);
		}

		//初始化头结点
		list<T>()
		{
			_phead = new _list_node<T>;
			_phead->_next = _phead->_prev = _phead;///头结点指向自身
		}
		//拷贝构造(深拷贝)
		list<T>(const list<T>& ls) {
			_phead = new _list_node<T>;
			_phead->_next = _phead->_prev = _phead;
			for (const auto& e : ls) {///这里是调用了operator=给e
				push_back(e);
			}
		}
		//operator=赋值
		list<T>& operator=(list<T> ls) {
			swap(_phead, ls._phead);
			return *this;
		}
		//插入
		void insert(iterator it, T val = T()) {
			assert(it._pnode);
			iterator left = iterator(it._pnode->_prev);
			node* newnode = new _list_node<T>(val);
			it._pnode->_prev = newnode;
			newnode->_prev = left._pnode;
			newnode->_next = it._pnode;
			left._pnode->_next = newnode;
		}
		void erase(iterator pos) {
			assert(pos._pnode);
			assert(pos._pnode->_prev != pos._pnode);///只有头结点的情况
			node* left = pos._pnode->_prev;
			node* right = pos._pnode->_next;
			left->_next = right;
			right->_prev = left;
			delete (pos._pnode);///释放空间
		}
		//尾插
		void push_back( T val=T() ) {
			insert(end(), val);
		}
		//尾删
		void pop_back() {
			erase(--end());
		}
		//头插
		void push_front( T val=T() ) {
			insert(begin(), val);
		}
		//头删;
		void pop_front() {
			erase(begin());
		}
		//统计size()--->效率很低,放个变量在private里记录就行。stl后面版本实现了
		size_t size() {
			size_t sz = 0;
			iterator it = begin();
			while (it != end()) {
				++sz;
				++it;
			}
			return sz;
		}
		//析构
		~list() {
			node* cur = _phead->_next;
			while (cur != _phead) {
				node* tmp = cur->_next;
				delete cur;
				cur = tmp;
			}
			delete _phead;
			_phead = nullptr;
			//测试析构
			/*node* tmp = _phead->_next;
			while (tmp != _phead) {
				cout << tmp->_val << " ";
				tmp = tmp->_next;
			}cout << endl;*/
		}
	private:
		node* _phead;//头结点
	};
};
template<typename T>
	void print_container(const list<T>& lt) {
		typename list<T>::const_iterator it = lt.cbegin();
		while (it != lt.cend()) {
			//(*it)++;
			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);
		print_container(lt);
		lt.push_front(8);
		lt.push_front(7);
		lt.push_front(6);
		lt.push_front(5);
		print_container(lt);
	}
	void test_list2() {
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

		list<int>lt2(lt);
		print_container(lt2);
	
		lt2.pop_back();
		print_container(lt2);
		print_container(lt);

		lt2.pop_front();
		print_container(lt2);
		print_container(lt);
		cout << lt2.size() << endl;
	}



	void test_list3() {
		class Date {
		public:
			int _year = 0;
			int _month = 0;
			int _day = 1;
		};
		list<Date> lt;
		lt.push_back(Date());
		lt.push_back(Date());
		lt.push_back(Date());

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

			it++;
		}cout << endl;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值