#include <iostream> #include <stdio.h> #include <stdlib.h> #define Maxsize 100 using namespace std; typedef struct SqStack{ int data[Maxsize]; int top; }SqStack; /* 栈顶指针:S.top,初始时设置S.top=-1,;栈顶元素:S.data[S.top]。 进栈操作:栈不满时,栈顶指针先加1,再送值到栈顶元素。 出栈操作:栈非空时,先取栈顶元素值,再将栈顶指针减一。 栈空条件:S.top==-1;栈满条件:S.top==Maxsize-1;栈长:S.top+1 */ //初始化 int Init_Sqstack(SqStack &S) { S.top=-1; } //进栈 bool push_SqStack(SqStack &S,int x) { if(S.top==Maxsize-1) return false;//栈满 S.data[++S.top]=x; return true; } //出栈 bool pop_SqStack(SqStack &S,int &x) { if(S.top==-1) return false; x=S.data[S.top]; S.top--; return true; } //获取栈顶元素 bool GetTop(SqStack S,int &x) { if(S.top==-1) return false; x=S.data[S.top]; return true; } void show_SqStack(SqStack S) { for(int i=0;i<=S.top;i++) { printf("%d ",S.data[i]); } } int main() { SqStack S; Init_Sqstack(S); push_SqStack(S,1); push_SqStack(S,2); push_SqStack(S,3); show_SqStack(S); int a=0; pop_SqStack(S,a); printf("%d \n",a); show_SqStack(S); GetTop(S,a); printf("%d \n",a); }