STL容器中list与迭代器iterator的模拟实现

list在容器中结构是有一个头结点_head,头结点指向第一个结点,尾结点指向头结点,它为双向循环链表,在其中它有自己的迭代器可以类似于智能指针,用于数据的访问和算法的配合。

代码实现:

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

template <class T>
struct _ListNode    //结点结构
{
	T _data;
	_ListNode* _prev;
	_ListNode* _next;

	_ListNode(const T& x)
		:_data(x)
		,_prev(NULL)
		,_next(NULL)
	{}
};

template <class T,class Ref,class Ptr>
struct _ListIterator    //迭代器的模拟实现
{
	typedef _ListNode<T> Node;
	typedef _ListIterator<T,Ref,Ptr> Self;
	Node* _node;

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

	_ListIterator(const Self &s)
		:_node(s._node)
	{}

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

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

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


	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;
	}
};

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* BuyNode(const T &x)
	{
		return new Node(x);
	}

	List()
		:_head(BuyNode(T()))
	{
		_head->_next=_head;
		_head->_prev=_head;
	}

	void PushBack(const T& x)  //尾插
	{
		Insert(End(),x);
	}

	void PopBack()     //尾删
	{
		Erase(--End());
	}
	
	void PushFront(const T& x)   //头插
	{
		Insert(Begin(),x);
	}

	void PopFront()   //头删
	{
		Erase(Begin());
	}

	bool Empty()const  //判空
	{
		return _head==_head->_next;
	}
	
	void Insert(Iterator pos, const T& x)  //指定位置插入
	{
		assert(pos._node);
		Node *tmp=BuyNode(x);
		Node *prev=pos._node->_prev;
		prev->_next=tmp;
		tmp->_prev=prev;
		tmp->_next=pos._node;
		pos._node->_prev=tmp;
	}

	template <typename InputIterator>
	void Insert(Iterator pos,InputIterator first,InputIterator last)  //指定位置插入一段区间
	{
		while(first!=last)
		{
			Insert(pos,*first);
			++first;
		}
	}
	
	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;
		Iterator it=pos;
		++it;
		delete pos._node;
		return it;
	}
    
    Iterator Begin()  //返回链表首位
	{
		return _head->_next;
	}

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

	Iterator End()  //返回链表末位下一位位置
	{
		return _head;
	}

	ConstIterator End()const
	{
		return _head;
	}

protected:
	Node *_head;
};

void PrintList(List<int> l)    //利用迭代器打印链表
{
	List<int>::Iterator it1=l.Begin();
	while(it1!=l.End())
	{
		cout<<(*it1)<<" ";
		++it1;
	}
	cout<<endl;
}
void FunTest()
{
	cout<<"自定义List:"<<endl;
	List<int> l1;
	//尾插
	l1.PushBack(1);
	l1.PushBack(2);
	l1.PushBack(3);
	l1.PushBack(4);
	l1.PushBack(5);
	PrintList(l1);
	//头插
	l1.PushFront(0);
	//迭代器访问链表数据
	PrintList(l1);
	l1.PopFront();  //头删
	PrintList(l1);
	l1.PopBack();//尾删
	PrintList(l1);
	l1.Insert(l1.Begin(),6);  //指定位置插入
	PrintList(l1);
	List<int>::Iterator it1=l1.Begin();
	++it1;
	int arr[]={10,20,30,40};
	l1.Insert(it1,arr,arr+4);
	PrintList(l1);
}

void Test()
{
	cout<<"库中list:"<<endl;
	list<int> l1;
	//尾插
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);
	l1.push_back(5);
	//头插
	l1.push_front(0);
	//迭代器访问链表数据
	list<int>::iterator it1=l1.begin();
	while(it1!=l1.end())
	{
		cout<<(*it1)<<" ";
		++it1;
	}
	cout<<endl;
	l1.pop_front();  //头删
	it1=l1.begin();
	while(it1!=l1.end())
	{
		cout<<(*it1)<<" ";
		++it1;
	}
	cout<<endl;
	l1.pop_back();//尾删
	l1.insert(l1.begin(),6);  //指定位置插入
	it1=l1.begin();
	while(it1!=l1.end())
	{
		cout<<(*it1)<<" ";
		++it1;
	}
	cout<<endl;
}
int main()
{
	//Test();
	FunTest();
	system("pause");
	return 0;
}

运行结果如图:


以上实现中主要实现了指定位置的插入删除,在头插,删,尾插,尾删等则可以直接调用它们来实现自己的功能。


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值