#include "iostream"
#define MAXSIZE 100
#define ERROR 1
#define OK 2
using namespace std;
typedef int SElemType;
typedef int Status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//创建一个栈空间
Status InitStack(SqStack &S)
{
S.base=new SElemType[MAXSIZE];
if(!S.base) return ERROR;
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
//进栈操作
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base==S.stacksize)return ERROR;
*S.top=e;
*S.top++;
}
//出栈操作
Status Pop(SqStack &S,SElemType e)
{
if(S.top==S.base)return ERROR;
*S.top--;
//*S.top=e;
return OK;
}
//获取栈元素
Status GetTop(SqStack &S)
{
if(S.top!=S.base)
{
return *(S.top-1);
}
}
//判断是否栈空
Status judegeNull(SqStack &S)
{
if(S.top==S.base)
{
cout<<"这个栈为空!"<<endl;
}
}
//判断是否栈满
Status judegeFull(SqStack &S)
{
if(S.top-S.base==S.stacksize)
{
cout<<"这个栈为满!"<<endl;
}
}
int main()
{
SqStack S;
int i,n;
InitStack(S);
cout<<"输入元素的个数:"<<endl;
cin>>n;
cout<<"这些元素是:"<<endl;
for(int i=1;i<=n;i++)
{
Push(S,i);
cout<<"进栈的顺序为:"<<i<<endl;
}
cout<<"出栈的顺序为:"<<endl;
while(S.top!=S.base){
//Pop(S,i);
cout<<*--S.top<<endl;
}
return 0;
}
栈的基本操作
最新推荐文章于 2024-08-09 10:25:03 发布