template <class T>
class ArrayStack
{
int size;
int top;
T * contain;
public:
ArrayStack():size(0),top(-1),contain(NULL){}
ArrayStack(int MaxSize);
void push(T& element);
T& GetTop();
T& Pop();
bool IsEmpty();
void MakeEmpty();
};
#include "ArrayStack.h"
template <class T>
ArrayStack<T>::ArrayStack(int MaxSize)
{
size=MaxSize;
top=-1;
contain=new T[size];
}
template <class T>
void ArrayStack<T>::push(T &element)
{
if(top==size-1)
{
cout<<"栈已满,无法继续入栈!"<<endl;
return;
}
else
{
contain[++top]=element;
}
}
template <class T>
T& ArrayStack<T>::GetTop()
{
if(top==-1)
{
cout<<"栈为空!"<<endl;
return;
}
else
{
return contain[top];
}
}
template <class T>
T& ArrayStack<T>::Pop()
{
if(top==-1)
{
cout<<"栈为空!"<<endl;
return;
}
else
{
return contain[top--];
}
}
template <class T>
bool ArrayStack<T>::IsEmpty()
{
return top==-1;
}
template <class T>
void ArrayStack<T>::MakeEmpty()
{
top=-1;
}
class ArrayStack
{
int size;
int top;
T * contain;
public:
ArrayStack():size(0),top(-1),contain(NULL){}
ArrayStack(int MaxSize);
void push(T& element);
T& GetTop();
T& Pop();
bool IsEmpty();
void MakeEmpty();
};
#include "ArrayStack.h"
template <class T>
ArrayStack<T>::ArrayStack(int MaxSize)
{
size=MaxSize;
top=-1;
contain=new T[size];
}
template <class T>
void ArrayStack<T>::push(T &element)
{
if(top==size-1)
{
cout<<"栈已满,无法继续入栈!"<<endl;
return;
}
else
{
contain[++top]=element;
}
}
template <class T>
T& ArrayStack<T>::GetTop()
{
if(top==-1)
{
cout<<"栈为空!"<<endl;
return;
}
else
{
return contain[top];
}
}
template <class T>
T& ArrayStack<T>::Pop()
{
if(top==-1)
{
cout<<"栈为空!"<<endl;
return;
}
else
{
return contain[top--];
}
}
template <class T>
bool ArrayStack<T>::IsEmpty()
{
return top==-1;
}
template <class T>
void ArrayStack<T>::MakeEmpty()
{
top=-1;
}
转载于:https://blog.51cto.com/lp4083331/203146