说明:
1.这个栈模板基于元素数组
2.未对代码作仔细的测试
#ifndef SUNXYSTACK_H
#define SUNXYSTACK_H
#define SMAX 200
template<class T>
class Stack
{public:
Stack();
Stack(int);
~Stack();
T& Top();
T& Pop();
bool Push(T&);
bool isEmpty();
bool isFull();
void Display();
private:
int top,size;
T* array;
};
template<class T>
Stack<T>::Stack()
{
this->Stack(SMAX);
}
template<class T>
Stack<T>::Stack(int s=SMAX)
{ if(s>SMAX||s<=0)
size=s;
else
size=SMAX
array=new T[size];
top=0;
}
template<class T>
Stack<T>::~Stack()
{
size=-1;top=0;
delete[] array;
}
template<class T>
T& Stack<T>::Top()
{
T& p=NULL;
if(!this->isEmpty())
p=array[top-1];
return p;
}
template<class T>
T& Stack<T>::Pop()
{
T& p=NULL;
if(!this->isEmpty())
{
top--;
p=array[top];
}
return p;
}
template<class T>
bool Stack<T>::Push(T& entry)
{
bool sign;
if(!this->isFull())
{
array[top]=entry;
top++;
sign=true;
}
else
sign=false;
return sign;
}
template<class T>
bool Stack<T>::isEmpty()
{
if(top==0)
return true;
else
return false;
}
template<class T>
bool Stack<T>::isFull()
{
if(top==size+1)
return true;
else
return false;
}
template<class T>
void Stack<T>::Display()
{
//add codes here
}
#endif