#include <iostream>
#include <cassert>
using namespace std;
template <class T>
struct Stack{
T *elem;
int length;
int MaxSize;
};
template <class T>
void InitStack(Stack<T> &S, int size) {
S.elem = new T[size];
S.length = 0;
S.MaxSize = size;
}
template <class T>
void DestroyStack(Stack<T> &S) {
if( S.MaxSize >0 )
delete[] S.elem;
}
template <class T>
void PushStack( Stack<T> &S, const T& e ) {
assert( S.length < S.MaxSize );
(S.elem)[S.length++] = e;
}
template <class T>
T PopStack( Stack<T> &S ) {
assert( S.length > 0 );
return (S.elem)[--S.length];
}
int main() {
int i;
Stack<int> stack;
InitStack( stack, 20 );
for( i=0;i<10;++i )
PushStack( stack, i );
for( i=0;i<10;++i )
cout<<PopStack(stack)<<endl;
DestroyStack( stack );
}
|