顺序栈:利用一组连续的存储单元依次存放自栈底到栈顶的数据元素;由于栈顶元素是经常变动的,所以附设top指示栈顶元素在顺序表中的位置,同时也需要知道顺序栈存储空间的起始位置,因此还需设定一个base指针用来指示栈空间的起始位置。
一般约定top指针指向栈顶元素的下一个位置,即新数据元素将要插入得位置。
下面我们使用模板简单实现一个顺序栈:
SeqStack.h
template<typename Type> class SeqStack{
public:
SeqStack(int sz):m_ntop(-1),m_nMaxSize(sz){
m_pelements=new Type[sz];
if(m_pelements==NULL){
cout<<"Application Error!"<<endl;
exit(1);
}
}
~SeqStack(){
delete[] m_pelements;
}
public:
void Push(const Type item); //push data
Type Pop(); //pop data
Type GetTop() const; //get data
void Print(); //print the stack
void MakeEmpty(){ //make the stack empty
m_ntop=-1;
}
bool IsEmpty() const{
return m_ntop==-1;
}
bool IsFull() const{
return m_ntop==m_nMaxSize-1;
}
priv