C++模板实现顺序栈模板实现顺序栈
顺序栈:利用一组连续的存储单元依次存放自栈底到栈顶的数据元素;由于栈顶元素是经常变动的,所以附设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;
}
private:
int m_ntop;
Type *m_pelements;
int m_nMaxSize;
};
template<typename Type> void SeqStack<Type>::Push(const Type item){
if(IsFull()){
cout<<"The stack is full!"<<endl;
return;
}
m_pelements[++m_ntop]=item;
}
template<typename Type> Type SeqStack<Type>::Pop(){
if(IsEmpty()){
cout<<"There is no element!"<<endl;
exit(1);
}
return m_pelements[m_ntop--];
}
template<typename Type> Type SeqStack<Type>::GetTop() const{
if(IsEmpty()){
cout<<"There is no element!"<<endl;
exit(1);
}
return m_pelements[m_ntop];
评论0
最新资源