自己写的模板,自己mark
#include <iostream>
#include <stdio.h>
#include <algorithm>
#define Maxsize 10
//设top始终指向栈顶
typedef struct SqStack
{
int top;
int data[Maxsize];
}SqStack;
void InitStack(SqStack &S)
{
S.top=-1;
}
bool StackEmpty(SqStack S)
{
if(S.top==-1)
return true;
else
return false;
}
bool StackFull(SqStack S)
{
if(S.top==Maxsize-1)
return true;
else
return false;
}
int Push(SqStack &S,int x)
{
if(S.top==Maxsize-1)
return 0;
S.top++;此处栈顶指针指的就是栈顶元素,栈顶指针先自增再把元素压进去。
S.data[S.top]=x;
return 1;
}
void Pop(SqStack &S,int &x)//这里要特别注意的就是Pop中x是引用的形式,
{
if(S.top==-1)
return false;
x=S.data[S.top];//用x返回
S.top--;
return true;
}
void GetTop(SqStack &S,int &x)
{
x=S.data[S.top];
}
void ClearStack(SqStack &S)
{
S.top=-1;
}
int main()
{
SqStack S;
int x;
InitStack(S);
bool l=StackEmpty(S);
printf("%d ",l);
Push(S,2);
GetTop(S,x);
printf("%d ",x);
Pop(S,x);
printf("%d",x);
return 0;
}