C++迭代器模拟

#include "stdafx.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <vector>
#include <list>
#include <algorithm>
#include <numeric>
#include <memory>
#include <sstream>
#include <fstream>
#include <ctype.h>
#include <dos.h>
#include <stdlib.h>
#include <iomanip>
#include <limits.h>
//#include "time.h"
//#include "omp.h"

using namespace std;


//   说明:声明一个栈 [10/30/2016 ZOSH];
template <typename T>
class Stack
{
private:

	//   说明:申明链表 [10/30/2016 ZOSH];
	typedef struct Node
	{
		T data;
		struct Node *next;
	} Node;

	//   说明:链表头结点 [10/30/2016 ZOSH];
	Node *top;
	//   说明:长度 [10/30/2016 ZOSH];
	unsigned int length;

public:
	//   说明:迭代器 [10/30/2016 ZOSH];
	class const_iterator{
		//friend class Stack<T>;
	private:
		//   说明:迭代器关联的链表 [10/30/2016 ZOSH];
		const Node *p;
	public:
		explicit const_iterator(Node *ptr);
		const T& operator*();
		const_iterator& operator++();
		const const_iterator operator++(int );        // const const_iterator<V>& operator--();        //const const_iterator<V>& operator--(int );
		bool operator == (const const_iterator& iter) const;
		bool operator != (const const_iterator& iter) const;
	};

	Stack();
	virtual ~Stack();

	unsigned int Size() const;

	void Push(T data);
	Node* Push();

	T Pop();
	T Top() const;

	const_iterator Begin() const;
	const_iterator End() const;

};

template <typename T>
Stack<T>::Stack():top(0), length(0)
{
	NULL;
};

template <typename T> 
typename Stack<T>::const_iterator Stack<T>::Begin() const
{
	return const_iterator::const_iterator(top);
};

template <typename T>
unsigned int Stack<T>::Size() const
{
	return length;
};

template <typename T>
void Stack<T>::Push(T data)
{
	Node *p = new Node;
	p->data = data;
	p->next = top;
	top = p;
	++length;
};

template <typename T>T 
typename Stack<T>::Pop()
{
	Node *p = top;
	T data = p->data;
	top = top->next;
	--length;
	delete p;
	return data;
};

template <typename T> T 
typename Stack<T>::Top() const
{
	return top->data;
};

template <typename T>
typename Stack<T>::const_iterator Stack<T>::End() const
{
	return const_iterator::const_iterator(0);
}


template <typename T> 
Stack<T>::~Stack()
{
	while(length)
	{
		Pop();
	}
}

//   说明:迭代器内部函数 [10/30/2016 ZOSH];
template <typename T> 
Stack<T>::const_iterator::const_iterator(Node *ptr):p(ptr)
{
	NULL;
}

template <typename T> 
const T& Stack<T>::const_iterator::operator*()
{   
	return this->p->data;
}

template <typename T> 
typename Stack<T>::const_iterator& Stack<T>::const_iterator::operator++()
{   
	p = p->next;    
	return *this;
}

template <typename T>  const 
typename Stack<T>::const_iterator Stack<T>::const_iterator::operator++(int )
{    
	const_iterator tmp = *this;
	p = p->next;   
	return tmp;

}

template <typename T>bool 
 Stack<T>::const_iterator::operator==(const const_iterator& iter) const
{    
	return p == iter.p;
}


template <typename T>bool 
 Stack<T>::const_iterator::operator!=(const const_iterator& iter) const
{    
	return !(p == iter.p);
}

//   说明:测试 [10/30/2016 ZOSH];
int _tmain(int argc, _TCHAR* argv[])
{
	Stack<int> stackTest;
	for (int i=0; i<4; i++)
	{
		stackTest.Push(i);
	}

	int x = stackTest.Top();

	Stack<int>::const_iterator iter = stackTest.Begin();

	for (iter=stackTest.Begin(); iter!=stackTest.End(); iter++) 
	{
		printf("%d", *iter);
	}

	return 0;
}


by 我执可破.  2016.10.30 于上海浦东.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值