#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 于上海浦东.