list的模拟实现(list的构造, 赋值运算符重载, 迭代器, 前置++ -- 后置++ --, 插入删除, 容量操作等)

list的模拟实现

list.h

#include 

using namespace std;

namespace gwp
{
	template 
	class ListNode
	{
	public:
		ListNode(const T& val = T())
			:m_prev(nullptr)
			, m_next(nullptr)
			, m_val(val)
		{

		}

	public:
		ListNode* m_prev;
		ListNode* m_next;
		T m_val;
	};
	template 
	class list
	{
	public:
		class iterator
		{
		public:
			iterator(ListNode* val = nullptr)
				:m_pNode(val)
			{

			}

			iterator(const iterator& l)
				:m_pNode(l.m_pNode)
			{

			}

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

			T& operator -> ()
			{
				return &m_pNode->m_val;
			}

			iterator operator ++ ()
			{
				m_pNode = m_pNode->m_next;
				return *this;
			}

			iterator operator ++ (int)
			{
				iterator tmp = *this;
				m_pNode = m_pNode->m_next;
				return tmp;
			}

			iterator operator -- ()
			{
				m_pNode = m_pNode->m_prev;
				return *this;
			}

			iterator operator -- (int)
			{
				iterator tmp = *this;
				m_pNode = m_pNode->m_prev;
				return tmp;
			}

			bool operator != (const iterator& l)
			{
				return m_pNode != l.m_pNode;
			}

			bool operator == (const iterator& l)
			{
				return m_pNode == l.m_pNode;
			}

		public:
			ListNode* m_pNode;
		};

		void createHead()
		{
			m_head = new ListNode;
			m_head->m_prev = m_head;
			m_head->m_next = m_head;
		}

		list()
		{
			createHead();
		}

		list(int n, const T& val = T())
		{
			createHead();
			
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		list(iterator start, iterator end)
		{
			createHead();

			insert(end(), start, end);
		}

		list(T* start, T* finish)
		{
			createHead();
			
			insert(end(), start, finish);
		}

		list(list& l)
		{
			createHead();

			insert(end(), l.begin(), l.end());
		}

		list& operator = (list& l)
		{
			if (this != &l)
			{
				clear();
				iterator it = l.begin();
				while (it != l.end())
				{
					push_back(*it);
					it++;
				}
			}
			return *this;
		}

		~list()
		{
			erase(begin(), end());
			delete m_head;
		}

		void clear()
		{
			erase(begin(), end());
		}

		void push_back(const T& val)
		{
			insert(end(), val);
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}

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

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

		iterator insert(iterator pos, const T& val)
		{
			ListNode* cur = new ListNode;
			ListNode* npos = pos.m_pNode;

			cur->m_val = val;

			cur->m_prev = npos->m_prev;
			cur->m_prev->m_next = cur;

			cur->m_next = npos;
			npos->m_prev = cur;

			return cur;
		}

		iterator insert(iterator pos, iterator start, iterator end)
		{
			iterator tmpit = --pos;
			pos++;

			for (iterator it = start; it != end; it++)
			{
				insert(pos, *it);
			}

			return ++tmpit;
		}

		iterator insert(iterator pos, T* start, T* finish)
		{
			iterator tmpit = --pos;
			pos++;

			for (T* it = start; it != finish; it++)
			{
				insert(pos, *it);
			}

			return ++tmpit;
		}

		iterator erase(iterator pos)
		{
			ListNode* npos = pos.m_pNode;
			ListNode* cur = npos->m_next;

			npos->m_next->m_prev = npos->m_prev;
			npos->m_prev->m_next = npos->m_next;
			delete npos;

			return cur;
		}

		iterator erase(iterator start, iterator finish)
		{
			iterator it = start;

			while (it != finish)
			{
				it = erase(it);
			}

			return finish;
		}

		iterator begin()
		{
			return m_head->m_next;
		}

		iterator end()
		{
			return m_head;
		}

		size_t size() const
		{
			int count = 0;

			ListNode* cur = m_head->m_next;
			while (cur != m_head)
			{
				count++;
				cur = cur->m_next;
			}

			return count;
		}

		bool empty() const
		{
			return size() ? false : true;
		}

		T& front()
		{
			return m_head->m_next->m_val;
		}

		const T& front() const
		{
			return m_head->m_next->m_val;
		}

		T& back()
		{
			return m_head->m_prev->m_val;
		}

		const T& back() const
		{
			return m_head->m_prev->m_val;
		}

		void swap(list& l)
		{
			swap(m_head, l.m_head);
		}

		void PrintList()
		{
			for (auto i = begin(); i != end(); i++)
			{
				cout << *i << ' ';
			}
			cout << endl;
		}



	public:
		ListNode* m_head;
	};
}

list.cpp

#include 
#include "list.h"

using namespace std;

void TestList1()
{
	gwp::list l1;
	gwp::list l2(10, 5);
	l2.PrintList();
	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	gwp::list l3(array, array + sizeof(array) / sizeof(array[0]));
	l3.PrintList();
	gwp::list l4(l3);
	l4.PrintList();
	l1 = l4;
	l1.PrintList();
}

void TestList2()
{
	// 测试PushBack与PopBack
	gwp::list l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.PrintList();
	l.pop_back();
	l.pop_back();
	l.PrintList();
	l.pop_back();
	cout << l.size() << endl;
	// 测试PushFront与PopFront
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	l.PrintList();
	l.pop_front();
	l.pop_front();
	l.PrintList();
	l.pop_front();
	cout << l.size() << endl;
}

void TestList3()
{
	int array[] = { 1, 2, 3, 4, 5 };
	gwp::list l(array, array + sizeof(array) / sizeof(array[0]));
	auto pos = l.begin();
	l.insert(l.begin(), 0);
	l.PrintList();
	++pos;
	l.insert(pos, 2);
	l.PrintList();
	l.erase(l.begin());
	l.erase(pos);
	l.PrintList();
	// pos指向的节点已经被删除,pos迭代器失效
	cout << *pos << endl;
	auto it = l.begin();
	while (it != l.end())
	{
		it = l.erase(it);
	}
	cout << l.size() << endl;
}

int main()
{
	gwp::list l(4, 5);
	int arr[5] = { 1, 3, 5, 7, 9 };
	gwp::list l2(arr, arr + 5);
	gwp::list l3 = l2;

	for (auto i = l3.begin(); i != l3.end(); i++)
	{
		cout << *i << ' ';
	}

	TestList1();
	TestList2();
	TestList3();

	system("pause");
	return 0;
}

你可能感兴趣的:(list的模拟实现(list的构造, 赋值运算符重载, 迭代器, 前置++ -- 后置++ --, 插入删除, 容量操作等))