C++初阶— list模拟实现源码

C++初阶— list模拟实现源码_第1张图片

要模拟实现list,必须要熟悉list的底层结构以及其接口的含义,参考文档

https://cplusplus.com/reference/list/list/list/

1.list模拟实现

    template
	struct  list_node
	{
		T _data;
		list_node* _next;
		list_node* _prev;

		list_node(const T& x = T())
			:_data(x)
			, _next(nullptr)
			, _prev(nullptr)
		{}
	};

	template
	struct __list_iterator
	{
		typedef list_node Node;
		typedef __list_iterator iterator;
		typedef bidirectional_iterator_tag iterator_category;
		typedef T value_type;
		typedef Ptr pointer;
		typedef Ref reference;
		typedef ptrdiff_t difference_type;

		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}
		bool operator!=(const iterator& it) const
		{
			return _node != it._node;
		}
		bool operator==(const iterator& it) const
		{
			return _node == it._node;
		}

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

		iterator& operator++()
		{
			_node =  _node->_next;
			return *this;
		}
		iterator operator++(int)
		{
			iterator tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		iterator& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		iterator operator--(int)
		{
			iterator tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		Ptr operator->()
		{
			return &(operator*());
		}
	};

	template
	class list
	{
		typedef list_node Node;
	public:
		typedef __list_iterator iterator;
		typedef __list_iterator const_iterator;
		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);
		}
		list()
		{
			empty_init();
		}
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		template 
		list(InputIterator first, InputIterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		void swap(list& lt)
		{
			std::swap(_head, lt._head);
		}
		list(const list& lt)
		{
			empty_init();
			list tmp(lt.begin(), lt.end());
			swap(tmp);
		}
		list& operator=(list lt)
		{
			swap(lt);
			return *this;
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);//返回下一个位置的迭代器
			}
		}
		void push_back(const T& x)
		{
			/*Node* tail = _head->_prev;
			Node* newNode = new Node(x);
			tail->_next = newNode;
			newNode->_prev = tail;
			newNode->_next = _head;
			_head->_prev = newNode;*/
			insert(end(), x);
		}void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_back()
		{
			erase(--end()); 
		}
		void pop_front()
		{
			erase(begin());
		}
		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newNode = new Node(x);
			prev->_next = newNode;
			newNode->_prev = prev;
			newNode->_next = cur;
			cur->_prev = newNode;
			return iterator(newNode);
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			return iterator(next);
		}
	private:
		Node* _head;
	};

2.测试用例

	void test_list1()
	{
		list lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);
		list::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
		it = lt.begin();
		while (it != lt.end())
		{
			*it *= 2;
			++it;
		}
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	struct Point
	{
		int _row;
		int _col;

		Point(int row = 0,int col = 0)
			:_row(row)
			,_col(col)
		{}
	};
	void test_list2()
	{
		list lt;
		lt.push_back(Point(10, 20));
		lt.push_back(Point(20, 30));
		lt.push_back(Point(30, 40));
		list::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << (*it)._row << ";" << (*it)._col<<"   ";
			cout << it->_row << ";" << it->_col << "   ";

			it++;
		}
		cout << endl;

	}

	void func(const list lt)
	{
		list::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
	void test_list3()
	{
		list lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(6);
		lt.push_back(8);
		lt.push_back(4);
		lt.push_back(5);
		func(lt);
		lt.push_front(10);
		lt.push_front(20);
		lt.push_front(30);
		lt.push_front(40);
		func(lt);
		lt.pop_back();
		lt.pop_back();
		func(lt);
		lt.pop_front();
		lt.pop_front();
		func(lt);

		auto pos = find(lt.begin(), lt.end(), 3);
		if (pos != lt.end())
		{
			//pos是否会失效?不会
			lt.insert(pos, 30);
		}
		func(lt);
		pos = find(lt.begin(), lt.end(), 3);
		if (pos != lt.end())
		{
			//pos是否会失效?会
			//链表按需申请释放节点,删除后,pos便是野指针
			lt.erase(pos);
		}
		func(lt);
	}

	void test_list4()
	{
		list lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(6);
		func(lt);
		list copy = lt;
		auto pos = find(lt.begin(), lt.end(), 3);
		if (pos != lt.end())
		{
			lt.erase(pos);
		}
		func(lt);
		func(copy);
	}
	//测试赋值
	void test_list5()
	{
		list lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(6);
		func(lt);
		list copy;
		copy = lt;
		auto pos = find(lt.begin(), lt.end(), 3);
		if (pos != lt.end())
		{
			lt.erase(pos);
		}
		func(lt);
		func(copy);
	}

你可能感兴趣的:(C++,c++,list,数据结构)