Files
Data_Structure/OrigFiles/3-特殊线性表/3-SqStack(顺序栈)/SqStack.h

90 lines
1.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
template <class DT>
struct SqStack // 顺序栈
{
DT *base; // 栈首址
int top; // 栈顶指针
int stacksize; // 栈容量
};
//基本操作的实现
//【算法3.1】 // 初始化栈
template <class DT>
void InitStack(SqStack<DT> &S, int m)
{
S.base=new DT[m]; // 申请栈空间
if(S.base==NULL) // 申请失败,退出
{
cout<<"未创建成功!";
exit (1);
}
S.top=-1; // 设置空栈属性
S.stacksize=m;
}
//算法3.2】 // 销毁栈
template <class DT>
void DestroyStack(SqStack<DT> &S)
{
delete [] S.base; // 释放栈空间
S.top=-1;
S.stacksize=0; // 设置栈属性
}
//【算法3.3】 // 入栈
template<class DT>
bool Push(SqStack<DT> &S,DT e)
{
if(S.top==S.stacksize-1) // 栈满,不能入栈
return false; // 返回false
S.top++;
S.base[S.top]=e;
return true; // 入栈成功返回true
}
//【算法3.4】 // 出栈
template<class DT>
bool Pop(SqStack<DT> &S,DT &e)
{
if(S.top==-1) // 栈空
return false; // 返回false
e=S.base[S.top]; // 取栈顶元素
S.top--; // 栈顶指针下移
return true; // 出栈成功返回true
}
//【算法3.5】 // 获取栈顶元素
template<class DT>
bool GetTop(SqStack<DT> S,DT &e)
{
if(S.top==-1) // 栈空
return false; // 返回false
e=S.base[S.top]; // 栈非空,取栈顶元素
return true; // 返回true
}
// 测栈空
template<class DT>
bool StackEmpty(SqStack<DT> S)
{
if(S.top==-1) // 空栈返回true
return true;
else // 空栈返回false
return false;
}
//显示栈内容
template<class DT>
void DispStack(SqStack<DT> S)
{
int i=S.top;
while(i>=0)
{
cout<<S.base[i--]<<"\t";
}
cout<<endl;
}