模拟实现List

#pragma once

namespace bite
{
	template<class T>
	struct ListNode
	{
		ListNode(const T& x = T())
		: prev(nullptr)
		, next(nullptr)
		, data(x)
		{}

		ListNode<T>* prev;
		ListNode<T>* next;
		T data;
	};

	template<class T>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T> Self;
		ListIterator(Node* pNode = nullptr)
			: _pNode(pNode)
		{ }

		//迭代器要具有指针类似的操作:* ->
		T& operator*()
		{
			return _pNode->data;
		}

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

		//迭代器要能够移动
		//前置++/后置++ 前置--/后置--
		Self& operator++()
		{
			_pNode = _pNode->next;
			return *this;
		}

		Self operator++(int)
		{
			Self temp(*this);
			_pNode = _pNode->next;
			return temp;
		}

		Self& operator--()
		{
			_pNode = _pNode->prev;
			return *this;
		}

		Self operator--(int)
		{
			Self temp(*this);
			_pNode = _pNode->prev;
			return temp;
		}
		//迭代器要能够比较
		bool operator!=(const Self& s)const
		{
			return _pNode != s._pNode;
		}

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

		Node* _pNode;
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		typedef ListIterator<T> iterator;

	//public:
	//	typedef T* iterator; ///思考是否可行?不可行

	public:
		list()
		{
			CreatHead();
		}

		list(int n, const T& data = T())
		{
			CreatHead();
			for (int i = 0; i < n; ++i)
				push_back(data);

		}

		template<class Iterator>
		list(Iterator first, Iterator last)
		{
			CreatHead();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		list(const list<T>& L)
		{
			CreatHead();

			Node* pCur = head->next;
			while (pCur != head)
			{
				push_back(pCur->data);
				pCur = pCur->next;
			}
		}

		list<T>& operator = (const list<T>& L);
		~list()
		{
			clear();
			delegate head;
			head = nullptr;
		}

		
		iterator begin()
		{
			return iterator(head->next);
		}

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

		/
		size_t size()const
		{
			size_t count = 0;
			Node* pCur = head->next;
			while (pCur != head)
			{
				++count;
				pCur = pCur->next;
			}

			return count;
		}

		bool empty()const
		{
			return head == head->next;
		}

		void resize(size_t newsize, const T& data = T())
		{
			size_t oldsize = size();
			if (newsize <= oldsize)
			{
				for (size_t i = newsize; i < oldsize; ++i)
					pop_back();
			}
			else
			{
				for (size_t i = oldsize; i < newsize; ++i)
					push_back(data);
			}
		}
		
		void push_back(const T& data)
		{
			insert(end(), data);
		}

		void pop_back()
		{
			erase(--end());
		}

		void push_front(const T& data)
		{
			insert(begin().data);
		}

		void pop_front()
		{
			erase(begin());
		}

		iterator insert(iterator pos, const T& data)
		{
			Node* newnode = new Node(data);
			Node* posNode = pos._pNode;

			newnode->next = posNode;
			newnode->prev = posNode->prev;
			posNode->prev = newnode;
			newnode->prev->next = newnode;

			return iterator(newnode);
		}

		iterator erase(iterator pos)
		{
			if (pos == end())
				return pos;
			Node* posNode = pos._pNode;
			Node* pRet = posNode->next;

			posNode->prev->next = posNode->next;
			posNode->next->prev = posNode->prev;

			delete posNode;

			return iterator(pRet);
		}

		iterator erase(iterator first, iterator last)
		{
			while (first != last)
			{
				first = erase(first);
			}

			return last;
		}
		void clear()
		{
			erase(begin(), end());
		}
	private:
		void CreatHead()
		{
			head = new Node;
			head->next = head;
			head->prev = head;
		}

	private:
		Node* head;
	};
}

#include <iostream>
using namespace std;

void TestList1()
{
	bite::list<int> L1;
	bite::list<int> L2(10, 5);

	int array[] = { 1, 2, 3, 4, 5 };
	bite::list<int> L3(array, array + sizeof(array) / sizeof(array[0]));

	bite::list<int> L4(L3);

	//迭代器
	auto it = L2.begin();
	while (it != L2.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	//注意:如果想要让自己写的容器支持范围for循环
	//那么该容器中必须要实现begin/end方法,以及迭代器++操作
	//范围for
	for (auto e : L3)
		cout << e << " ";
	cout << endl;
}

void TestList2()
{
	bite::list<int> L;
	L.push_back(1);
	L.push_back(2);
	L.push_back(3);
	L.push_back(4);
	L.push_back(5);

	cout << L.size() << endl;
	L.resize(10, 6);
	for (auto e : L)
		cout << e << " ";
	cout << endl;

	L.resize(4);
	for (auto e : L)
		cout << e << " ";
	cout << endl;

	L.push_front(0);
	for (auto e : L)
		cout << e << " ";
	cout << endl;

	L.pop_front();
	for (auto e : L)
		cout << e << " ";
	cout << endl;

	L.erase(L.begin());
	for (auto e : L)
		cout << e << " ";
	cout << endl;

	L.clear();
	if (L.empty())
	{
		cout << "ok" << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值