【STL】模拟实现list

本文详细介绍了一种基于C++的双向链表实现方案,并通过具体示例展示了如何利用迭代器进行元素的插入、删除等操作。此外,还提供了完整的测试代码以验证实现的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

list是标准模板库中的一个容器,实际上是一条带头节点的双向链表。通过与迭代器的组合使用,使得工作效率大大提高。

要注意:迭代器只是为了访问、修改和遍历对象,不对空间进行管理。

#pragma once 
#include <iostream>
using namespace std;

//定义链表结点结构体
template<class T>
struct __ListNode
{
	T _data;
	__ListNode<T>* _next;
	__ListNode<T>* _prev;

	__ListNode(const T& d)
		:_data(d)
		, _next(NULL)
		, _prev(NULL)
	{}
};

//迭代器
template<class T,class Ref,class Ptr>
struct __ListIterator
{
	typedef __ListNode<T> Node;
	typedef __ListIterator<T, Ref, Ptr> Self;

	__ListIterator(Node* node)
		:_node(node)
	{}

	__ListIterator()
	{}

	Ref operator*()    //解引用的重载
	{
		return _node->_data;
	}

	Self& operator++()  //前置++,像指针一样可以移动一位
	{
		_node = _node->_next;
		return *this;
	}

	Ptr operator->()   //重载->
	{
		return &(*_node)._data;
	}

	Self operator++(int)   //后置++
	{
		Node* cur = _node;
		_node = _node->_next;
		return cur;
	}

	Self& operator--()
	{
		_node = _node->_prev;
		return *this;
	}

	Self operator--(int)
	{
		Node* cur = _node;
		_node = _node->_prev;
		return cur;
		//return Self(cur);//先构造在拷贝,编译器优化,效率高
	}

	bool operator==(const Self& s) const
	{
		return this->_node == s._node;
	}

	bool operator!=(const Self& s) const
	{
		return this->_node != s._node;
	}

	Node* _node;
};

//模拟实现list
template<class T>
class List
{
	typedef __ListNode<T> Node;
public:
	typedef __ListIterator<T, T&, T*> Iterator;
	typedef __ListIterator<T, const T&, const T*> ConstIterator;

	Node* GetNode(const T& d)  //申请结点
	{
		return new Node(d);
	}

	List()    //双向循环列表
	{
		_head = GetNode(T());//匿名对象
		_head->_next = _head;
		_head->_prev = _head;
	}

	~List()
	{
		if (_head->_next == _head)
		{
			delete _head;
			_head = NULL;
		}
	}

	void PushBack(const T& d)  //后插,学会代码复用
	{
		/*Node* tail = _head->_prev;
		Node* tmp = GetNode(d);

		tail->_next = tmp;
		tmp->_prev = tail;
		tmp->_next = _head;
		_head->_prev = tmp;*/

		Insert(End(), d);
	}

	void PopBack()   //后删
	{
		if (_head->_next == _head)
		{
			return;
		}

		Erase(--End());

		/*Node* del = _head->_prev;
		Node* prev = del->_prev;
		_head->_prev = prev;
		prev->_next = _head;
		delete del;*/
	}

	void PushFront(const T& d)  //前插
	{
		Insert(Begin(), d);
	}

	void PopFront()     //前删
	{
		if (_head->_next == _head)
		{
			return;
		}
		else
		{
			Erase(Begin());
		}
	}

	//在pos前面进行插入
	void Insert(Iterator pos, const T& d)
	{
		assert(pos._node);

		Node* newnode = GetNode(d);
		Node* prev = pos._node->_prev;
		Node* cur = pos._node;

		prev->_next = newnode;
		newnode->_next = cur;
		newnode->_prev = prev;
		cur->_prev = newnode;
	}

	//删除指定位置的结点
	Iterator Erase(Iterator& pos)  //注意迭代器失效的问题
	{
		assert(pos._node&&pos._node != _head);

		Node* prev = pos._node->_prev;
		Node* next = pos._node->_next;

		prev->_next = next;
		next->_prev = prev;

		delete pos._node;
		return next;
	}

	template <typename InputIterator>
	void Insert(Iterator pos, InputIterator first, InputIterator last)  //指定位置插入一段区间  
	{
		while (first != last)
		{
			Insert(pos, *first);
			++first;
		}
	}

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

	Iterator Begin()   
	{
		return Iterator(_head->_next);
	}

	ConstIterator Begin() const
	{
		return ConstIterator(_head->_next);
	}

	Iterator End()
	{
		return Iterator(_head);
	}

	ConstIterator End() const
	{
		return ConstIterator(_head);
	}

private:
	Node* _head;    //指向头结点的指针
};

void PrintList(const List<int>& l)
{
	List<int>::ConstIterator it = l.Begin();

	while (it != l.End())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

测试:

void TestList()
{
	List<int> l1;

	l1.PushBack(1);
	l1.PushBack(2);
	l1.PushBack(3);
	l1.PushBack(4);
	PrintList(l1);

	l1.PopBack();
	l1.PopBack();
	l1.PopBack();
	l1.PopBack();
	l1.PopBack();
	PrintList(l1);
}

void TestList1()
{
	List<int> l1;    //4 3 2 1
	l1.PushFront(1);
	l1.PushFront(2);
	l1.PushFront(3);
	l1.PushFront(4);
	PrintList(l1);

	
	List<int>::Iterator it = l1.Begin();
	++it;
	l1.Insert(it, 10);    //4 10 3 2 1
	PrintList(l1);

	++it;
	it = l1.Erase(it);    //4 10 3 1
	PrintList(l1);

	l1.PopFront();
	l1.PopFront();
	l1.PopFront();
	l1.PopFront();
	l1.PopFront();
	PrintList(l1);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值