iterator的实现

List.h的内容

#pragma once

#define DATA int
#define NULL 0

typedef struct SNODE
{
	DATA d;
	struct SNODE *pre, *next;
}SNode;

class List
{
private:
	SNode *Header, *Tail;
	int nCount;
public:
	List();
	~List();
	void push_back(DATA d);
	void push_front(DATA d);
	void clear();
	SNode *begin()const;
	SNode *end()const;
	SNode *rbegin()const;
	SNode *rend()const;
	DATA  front()const;
	DATA  back()const;
	void pop_front();
	void pop_back();
	int size()const;
	void erase(DATA d);
public:
	class iterator
	{
	public:
		iterator(SNode *s);
		~iterator();
		void operator=(SNode *s);
		DATA operator*();
		iterator &operator++();
		iterator operator++(DATA);
		iterator &operator--();
		iterator operator--(DATA);
		operator SNode*();
	private:
		SNode *p;
	};

};

List.cpp的内容

#include "List.h"

List::List()
{
	Header = Tail = nullptr;
	nCount = 0;

	SNode *p = new SNode;
	p->d = NULL;
	p->next = p->pre = nullptr;
	Header = Tail = p;
}


List::~List()
{
}

/*尾部插入*/
void List::push_back(DATA d)
{
	SNode *pNew = new SNode;
	pNew->pre = nullptr;
	pNew->next = nullptr;
	pNew->d = d;

	/*
	环形链表
	1、当Header = Tail时,Tail不动,Header移动到第一个节点
	2、寻找尾节点,再在尾节点添加节点
	3、把添加的节点指向Tail
	*/
	if (Header == Tail)    
	{
		pNew->pre = Header;
		pNew->next = Tail;
		Header = pNew;
	}
	else
	{
		SNode *p = nullptr;

		//方法一:
		/*p = Header;
		while (p->next != Tail)
			p = p->next;
		p->next = pNew;
		pNew->pre = p;
		pNew->next = Tail;
		p = pNew;*/

		//方法二:从Tail往前推一个地址就是尾节点
		p = Tail->pre;
		p->next = pNew;
		pNew->pre = p;
		pNew->next = Tail;
	}
	Tail->pre = pNew;
	nCount++;
	/*双向链表*/
	//if (Tail)
	//	Tail->next = pNew;
	//else
	//	Header = pNew;
	//pNew->pre = Tail;
	//pNew->next = nullptr;
	//Tail = pNew;
}

/*头部插入*/
void List::push_front(DATA d)
{
	SNode *pNew = new SNode;
	pNew->d = d;
	pNew->next = pNew->pre = nullptr;

	if (Header == Tail)
	{
		pNew->pre = Tail;
		pNew->next = Header;
		Tail->pre = pNew;
	}
	else
	{
		SNode *p = nullptr;
		p = Header;
		p->pre = pNew;
		pNew->next = p;
	}
	Header = pNew;

	nCount++;
}

/*获取头结点地址*/
SNode *List::begin()const
{
	return Header;
}

/*反向打印头结点*/
SNode *List::rbegin()const
{
	return Tail->pre;
}

/*获取尾结点地址*/
SNode *List::end()const
{
	return Tail;
}

/*反向打印尾结点*/
SNode *List::rend()const
{
	return Header->pre;
}

/*获取节点个数*/
int List::size()const
{
	return nCount;
}

/*获取头部元素*/
DATA List::front()const
{
	return Header->d;
}

/*获取尾部元素*/
DATA List::back()const
{
	return Tail->pre->d;
}

/*删除头部节点*/
void List::pop_front()
{
	SNode *Del = nullptr;
	Del = Header;
	Header = Header->next;  //头结点偏移至第二节点
	Header->pre = Tail;      //头结点前驱指向Tail
	nCount--;
	delete Del;
}

/*尾部删除*/
void List::pop_back()
{
	SNode *Del = nullptr,*p = nullptr;
	p = Tail->pre;
	Del = p;
	p = p->pre;
	p->next = Tail;   //尾节点后驱指向Tail
	Tail->pre = p;    //Tail前驱指向尾节点
	nCount--;
	delete Del;
}

/*删除元素*/
void List::erase(DATA d)
{
	SNode *Del = nullptr,*p = nullptr;
	p = Header;
	while (p)
	{
		if (p->d == d)
		{
			Del = p;
			p = p->pre;
			p->next = Del->next;
			Del->next->pre = p;
			delete Del;
			break;
		}
		p = p->next;
	}
}

void List::clear()
{
	SNode *Del = nullptr, *p = nullptr;
	p = Header;
	while (p)
	{
		Del = p;
		p = p->next;
		delete Del;
	}
}

/*在初始化时,获取begin函数的返回值--- list::iterator it = list.begin() */
List::iterator::iterator(SNode *s)
{
	this->p = s;
}

List::iterator::~iterator()
{
}

void List::iterator::operator=(SNode *s)
{
	this->p = s;
}

/*获取结点里的数据--- *it */
DATA List::iterator::operator*()
{
	return this->p->d;
}

/*获取下一个结点地址--- ++it*/
List::iterator &List::iterator::operator++()
{
	p = p->next;
	return *this;
}

/*获取下一个结点地址--- it++*/
List::iterator List::iterator::operator++(DATA)
{
	List::iterator it(p);
	p = p->next;
	return it;
}

/*前一个节点的地址--- --it*/
List::iterator &List::iterator::operator--()
{
	p = p->pre;
	return *this;
}

/*前一个节点的地址--- it--*/
List::iterator List::iterator::operator--(DATA)
{
	List::iterator it(p);
	p = p->pre;
	return it;
}

/*返回SNode的数据*/
List::iterator::operator SNode *()
{
	return p;
}

主函数的内容

#include"List.h"
#include<iostream>
using namespace std;

int main()
{
	List list;
	list.push_back(22);
	list.push_back(23);
	list.push_back(24);
	list.push_back(25);
	list.push_front(26);
	list.push_front(27);
	List::iterator it = list.begin();
	while (it != list.end())
	{
		cout << *it << endl;
		++it;
	}
	cout << "节点个数" << list.size() << endl;
	cout << "反向打印" << endl;
	list.pop_front();
	list.pop_back();
	list.erase(22);
	it = list.rbegin();
	while (it != list.rend())
	{
		cout << *it << endl;
		--it;
	}
	list.clear();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38611124/article/details/81078666