#include<iostream>
#define MaxSize 10
using namespace std;
typedef struct{
int data[MaxSize];
int top;
}SqStack;
//初始化
void InitSqStack(SqStack &S){
S.top = -1;
}
//判空
bool StackEmpty(SqStack S){
if(S.top == -1) return true;
else return false;
}
//进栈
bool Push(SqStack &S,int x){
if(S.top == MaxSize-1)
return false;
S.top = S.top+1;
S.data[S.top] = x;
return true;
}
//出栈
bool Pop(SqStack &S,int &x){
if(S.top == -1)
return false;
x = S.data[S.top];
S.top--;
return true;
}
int main(){
SqStack S;
InitSqStack(S);
S.data[0] = 1;
S.data[1] = 2;
S.top = 1;
Push(S,3);
cout << S.data[2] << endl;
int x;
Pop(S,x);
cout << x << endl;
return 0;
}
C++顺序栈
于 2022-05-22 20:46:15 首次发布