list的模拟实现(C++)

#include <iostream>
#include <assert.h>
using namespace std;
template <class T>

//双向链表
struct ListNode{
	ListNode(const T& val = T())
	:_next(nullptr)
	,_prev(nullptr)
	,_val(val)
	{}
	ListNode<T>* _next;
	ListNode<T>* _prev;
	T _val;
};
//迭代器
template<class T,class Ref,class Ptr>
struct ListIterator{
	typedef ListNode<T>* pNode;
	typedef ListIterator<T,Ref,Ptr> self;
	pNode _node;
	//构造函数
	ListIterator(pNode node)
		:_node(node)
	{}
	//重载+
	self operator+(const T& n){
		for (int i = 0; i < n; i++){
			_node = _node->_next;
		}
		return *this;
	}
	//重载前置++
	self operator++(){
		_node = _node->_next;
		return *this;
	}
	//重载后置++,返回没有改变之前的值,然后自身++
	self operator++(int){
		self tmp(*this);
		_node = _node->_next;
		return *this;
	}
	//重载前置--
	self& operator--(){
		_node = _node->_prev;
		return *this;
	}
	//重载后置--
	self operator--(int){
		self tmp(*this);
		_node = _node->_prev;
		return *this;
	}
	//重载*
	Ref operator*(){
		return _node->_val;
	}
	//重载->
	Ptr operator->(){
		return &(operator*());
	}
	//重载!=,比较两个迭代器封装的结点是否一样
	bool operator!=(const self l){
		return _node != l._node;
	}
	//重载==
	bool operator==(const self l){
		return _node == l._node;
	}
};
template<class T>
class List{
public:
	typedef ListNode<T> Node;
	typedef ListNode<T>* pNode;
	//const对象不能调用非const对象成员函数,只能调用const成员函数
	//但是const成员函数operator++,operator--不能改变_node的值
	//导致const不能执行++,--
	typedef ListIterator<T, T&, T*> iterator;
	typedef ListIterator<T, const T&, const T*> const_iterator;
	//构造函数
	List(const T& val = T())
		:_head(new Node(val))
	{
		_head->_prev = _head;
		_head->_next = _head;
	}
	//拷贝构造函数
	//list需要实现深拷贝,否则浅拷贝只会拷贝对象模型中的头指针,不会拷贝
	//结点,析构函数释放结点的时候会造成二次释放,导致程序崩溃
	List(const List<T>& l){
		//首先创建头指针
		_head = new Node;
		_head->_prev = _head;
		_head->_next = _head;
		//遍历一遍,插入到list对象中去
		for (auto e : l){
			Push_Back(e);
		}
	}
	//赋值运算符
	List<T>& operator=( List<T>& l){
		传统写法
		//if (this != &l){
		//	Clear();
		//	for (const auto& e : l){
		//		Push_Back(e);
		//	}
		//}
		//return *this;
		swap(_head, l._head);
		return *this;
	}
	
	//尾插,更新指向
	void Push_Back(const T& val ){
		pNode CurNode = new Node(val);
		assert(CurNode);
		pNode prev = _head->_prev;
		prev->_next = CurNode;
		CurNode->_prev = prev;
		CurNode->_next = _head;
		_head->_prev = CurNode;
	}
	//头插
	void PushFront(const T& val){
		Insert(begin(), val);
	}
	//头删
	void PopFront(){
		Erase(begin());
	}
	//尾删
	void PopBack(){
		Erase(--end());
	}
	//迭代器
	iterator begin(){
		return iterator(_head->_next);
	}
	iterator end(){
		return iterator(_head);
	}
	//const迭代器
	const_iterator begin()const{
		return const_iterator(_head->_next);
	}
	const_iterator end()const{
		return const_iterator(_head);
	}
	//在pos前面插入val
	void Insert(iterator pos, const T& val){
		pNode NewNode = new Node(val);
		pNode cur = pos._node;
		pNode prev = cur->_prev;
		prev->_next = NewNode;
		NewNode->_prev = prev;
		NewNode->_next = cur;
		cur->_prev = NewNode;
	}
	//删除指定结点的值
	//Erase:迭代器失效
	//获取Erase的返回值(当前被删除结点的下一个位置),更新迭代器
	iterator Erase(iterator pos){
		if (pos != end()){
			pNode cur = pos._node;
			pNode prev = cur->_prev;
			pNode next = cur->_next;
			prev->_next = next;
			next->_prev = prev;

			delete cur;
			//更新pos的位置
			pos = iterator(next);
		}
		return pos;
	}
	//删除全部
	void Clear(){
		if (_head){
			pNode cur = _head->_next;
			while (cur != _head){
				pNode next = cur->_next;
				delete cur;
				cur = next;
			}
			_head->_next = _head;
			_head->_prev = _head;
		}
	}
	//析构函数
	~List(){
		Clear();
		if (_head){
			delete _head;
			_head = nullptr;
		}
	}

private:
	pNode _head;
};
//构建date类
//class Date{
//	int _year;
//	int _month;
//	int _day;
//};
template<class T>
void PrintList(const List<T>& l){
	auto lit = l.begin();
	while (lit != l.end())
	{
		//*lit = 10;
		cout << *lit << " ";
		++lit;
	}
	cout << endl;
}

int main(){
	List<int> l;
	l.Push_Back(2);
	l.Push_Back(3);
	l.Push_Back(4);
	l.Push_Back(5);
	l.Push_Back(6);
	//l.PopBack();
	//l.Insert(l.begin(), 5);
	l.Clear();
	//PrintList(l);
	//l.Erase(l.begin()+2);
	//PrintList(l);
	//l.PushFront(6);
	//l.PopFront();
	auto lit = l.begin();
	while (lit != l.end())
	{
		
		//*lit = 10;
		cout << *lit << " ";
		++lit;
	}
	/*PrintList(l);*/
	cout << endl;
	system("pause");
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值