STL之list的模拟实现

实现的功能:迭代器、反向迭代器、以及list的各种接口

list.h

namespace myList
{
	List的结点类
	template<class T>
	class ListNode
	{
	public:
		ListNode(const T& val = T())
			:_data(val)
			, _next(nullptr)
			, _prev(nullptr)
		{}
		T _data;
		ListNode<T>* _next;
		ListNode<T>* _prev;
	};

	//List的迭代器
	template<class T, class Ref, class Ptr>		//
	class ListIterator
	{
	public:
		typedef ListNode<T>* PNode;
		typedef ListIterator<T, Ref, Ptr> Self;
		PNode _node;

		ListIterator(PNode node)
			:_node(node)
		{}

		//*iterator
		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &(_node->_data);
		}

		//++iterator _node->_next
		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& lit)
		{
			return _node != lit._node;
		}

		bool operator==(const Self& lit)
		{
			return _node == lit._node;
		}
	};

	//List的反向迭代器
	template<class T, class Ref, class Ptr, class Iterator>	//传参的时候根据模板推演参数类型
	class ListReverseIterator
	{
		typedef ListReverseIterator<T, Ref, Ptr, Iterator> Self;
	public:
		ListReverseIterator(const Iterator& it)
			:_it(it)
		{}

		ListReverseIterator(const Self& s)
			:_it(s._it)
		{}

		Ref operator*()
		{
			Iterator tmp = _it;
			return *(--tmp);	//正向迭代器和反向迭代器是前后的。反向迭代器当前位置的下一个位置是正向迭代器的当前位置。
		}

		Ptr operator->()
		{
			return &operator*();
		}

		//前置++,反向迭代器的++就是正向迭代器的--
		Self& operator++()	//前置返回引用
		{
			--_it;
			return *this;
		}

		Self operator++(int)
		{
			Iterator temp(_it);		//调用正向迭代器的构造方法
			--_it;
			return temp;
		}

		Self& operator--()
		{
			++_it;
			return *this;
		}

		Self operator--(int)
		{
			Iterator temp(_it);
			++_it;
			return temp;
		}

		bool operator==(const Self& s)
		{
			return _it == s._it;
		}

		bool operator!=(const Self& s)
		{
			return _it != s._it;
		}

	private:
		Iterator _it;		//封装正向迭代器,底层调用的是const和非const的正向迭代器
	};

	template<class T>
	class List
	{
	public:
		typedef ListNode<T> Node;
		typedef Node* PNode;
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;
		typedef ListReverseIterator<T, const T&, const T*, iterator> reverse_iterator;
		typedef ListReverseIterator<T, const T&, const T*, const_iterator> const_reverse_iterator;
		List()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		List(int n, const T& value = T())
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			for (int i = 0; i < n; ++i){
				PushBack(value);
			}
		}

		template<class Iterator>
		List(Iterator first, Iterator last)
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			while (first != last){
				PushBack(*first);
				++first;
			}
		}

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

			List<T> temp(l.cbegin(), l.cend());
			swap(_head, temp._head);
		}

		List<T>& operator=(List<T> l)	//现代版写法,传值会调用构造,创建出临时变量,交换this的指针指向,
			//临时变量出作用域会销毁
		{
			swap(_head, l._head);
			return *this;
		}

		~List()
		{
			if (_head)
			{
				PNode cur = _head->_next;
				while (cur != _head)
				{
					PNode next = cur->_next;
					delete cur;
					cur = next;
				}
				delete _head;
				_head = nullptr;
			}
		}

		
		//迭代器
		iterator begin()
		{
			return iterator(_head->_next);
		}

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

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

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

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());	//end()传入相当传入iterator(_head)然后调用反向迭代器的构造函数
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

		const_reverse_iterator crbegin() const
		{
			return const_reverse_iterator(cend());
		}

		const_reverse_iterator crend() const
		{
			return const_reverse_iterator(cbegin());
		}

		///
		//List Access
		T& Front()
		{
			return _head->_next->_data;
		}

		const T& Front() const
		{
			return _head->_next->_data;
		}

		T& Back()
		{
			return _head->_prev->_data;
		}

		const T& Back() const
		{
			return _head->_prev->_data;
		}

		/
		//List Modify
		void PushBack(const T& val)
		{
			PNode newNode = new Node(val);
			PNode prev = _head->_prev;
			prev->_next = newNode;
			newNode->_prev = prev;
			newNode->_next = _head;
			_head->_prev = newNode;
		}

		void PopBack()
		{
			//找到要删除的结点,也就是头结点的前一个就是最后一个结点
			PNode del = _head->_prev;
			//只有一个头结点不能删除
			if (del != _head){
				_head->_prev = del->_prev;
				del->_prev->_next = _head;
				delete del;
			}
		}
		void PushFront(const T& val)
		{
			PNode newnode = new Node(val);
			newnode->_prev = _head;
			newnode->_next = _head->_next;
			_head->_next->_prev = newnode;
			_head->_next = newnode;
		}
		void PopFront()
		{
			PNode cur = _head->_next;
			if (cur != _head){
				_head->_next = cur->_next;
				cur->_next->_prev = _head;
				delete cur;
			}
		}
		//在pos位置前插入值为val的结点
		iterator Insert(iterator pos, const T& val)
		{
			PNode newnode = new Node(val);
			//pos是迭代器,要取到pos位置的结点
			PNode cur = pos._node;
			newnode->_next = cur;
			newnode->_prev = cur->_prev;
			newnode->_prev->_next = newnode;
			cur->_prev = newnode;

			return iterator(newnode);
		}
		//删除pos位置的结点,返回该结点的下一个位置
		iterator Erase(iterator pos)
		{
			//保存pos位置的结点以及pos位置结点的前一个结点和后一个结点
			PNode del = pos._node;
			PNode prev = del->_prev;
			PNode next = del->_next;
			//注意不能删除头结点
			if (del != _head){
				prev->_next = next;
				next->_prev = prev;

				delete del;
				del = nullptr;

				//更新迭代器
				pos = iterator(next);
			}
			return pos;
		}
		void Clear()
		{
			PNode cur = _head->_next;
			while (cur != _head){
				PNode next = cur->_next;
				delete cur;
				cur = next;
			}
			_head->_next = _head;
			_head->_prev = _head;
		}
		//
		//List Capacity
		size_t Size() const
		{
			size_t count = 0;
			PNode cur = _head->_next;
			while (cur != _head){
				++count;
				cur = cur->_next;
			}
			return count;
		}

		bool Empty() const
		{
			return _head->_next == _head;
		}

		void ReSize(size_t newSize, const T& val = T())
		{
			size_t oldSize = Size();
			if (oldSize <= newSize){
				for (size_t i = oldSize; i < newSize; ++i){
					PushBack(val);
				}
			}
			else{
				for (size_t i = newSize; i < oldSize; ++i){
					PopBack();
				}
			}
		}
	private:
		PNode _head;
	};
}

测试list
test.cpp

#include<iostream>
#include "list.h"
using namespace std;

//正向打印链表
template<class T>
void PrintList(myList::List<T>& l)
{
	auto lit = l.begin();
	while (lit != l.end()){
		cout << *lit << " ";
		++lit;
	}
	cout << endl;
}

//反向打印链表
template<class T>
void PrintListReverse(const myList::List<T>& l)
{
	auto it = l.crbegin();
	while (it != l.crend()){
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

//测试List的构造
void Test1()
{
	//myList::List<int> l1;
	//l1.PushBack(1);
	//l1.PushBack(2);
	//l1.PushBack(3);
	//l1.PushBack(4);
	//myList::List<int> l2;
	//l2 = l1;
	//PrintList(l1);
	//PrintList(l2);
	myList::List<int> l1;
	myList::List<int> l2(10, 5);
	PrintList(l2);

	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	myList::List<int> l3(array, array + sizeof(array) / sizeof(array[0]));
	PrintList(l3);

	myList::List<int> l4(l3);
	PrintList(l4);

	l1 = l4;
	PrintList(l1);
	PrintListReverse(l1);
	
	char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
	myList::List<char> l5(arr, arr + sizeof(arr) / sizeof(arr[0]));
	PrintList(l5);
}

//测试PushBack()/PopBack()/PushFront()/PopFront()
void Test2()
{
	//PushBack()/PushFront()
	myList::List<int> l;
	l.PushBack(1);
	l.PushBack(2);
	l.PushBack(3);
	PrintList(l);

	l.PopBack();
	l.PopBack();
	PrintList(l);

	l.PopBack();
	cout << l.Size() << endl;
	//PushFront()/PopFront()
	l.PushFront(1);
	l.PushFront(2);
	l.PushFront(3);
	PrintList(l);

	l.PopFront();
	l.PopFront();
	PrintList(l);

	l.PopFront();
	cout << l.Size() << endl;
}

//Insert()/Erase()
void Test3()
{
	int array[] = { 1, 2, 3, 4, 5 };
	myList::List<int> l(array, array + sizeof(array) / sizeof(array[0]));
	auto pos = l.begin();
	l.Insert(pos, 0);
	PrintList(l);

	++pos;
	l.Insert(pos, 7);
	PrintList(l);

	l.Erase(l.begin());
	auto pos2 = l.Erase(pos);
	PrintList(l);
	
	//pos指向的结点已经被删除,pos迭代器失效
	cout << *pos << endl;
	cout << *pos2 << endl;

	auto it = l.begin();
	while (it != l.end()){
		it = l.Erase(it);
	}
	cout << l.Size() << endl;
}

//ReSize()/Clear()
void Test4()
{
	int array[] = { 1, 2, 3, 4, 5 };
	myList::List<int> l(array, array + sizeof(array) / sizeof(array[0]));
	cout << l.Size() << endl;
	PrintList(l);

	l.ReSize(10, 7);
	cout << l.Size() << endl;
	PrintList(l);

	l.ReSize(4);
	cout << l.Size() << endl;
	PrintList(l);

	l.ReSize(3, 0);
	cout << l.Size() << endl;
	PrintList(l);

	l.Clear();
	cout << l.Size() << endl;
	PrintList(l);
}

int main()
{
	//Test1();
	//Test2();
	//Test3();
	Test4();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值