考研 ds 顺序栈
作者:
smile_922
,
2024-09-21 18:29:05
,
所有人可见
,
阅读 7
#include<iostream>
using namespace std;
const int maxsize = 10;
typedef struct Stack {
int data[maxsize];
int top = 0;
} SqStack;
bool Is_full(SqStack &s) {
return s.top == 9;
}
bool Is_empty(SqStack &s) {
return s.top == 0;
}
bool push(SqStack &s, int x) {
if(Is_full(s))return false;
s.data[++s.top] = x;
return true;
}
bool pop(SqStack &s, int &x) {
if(Is_empty(s))return false;
x = s.data[--s.top];
return true;
}
void print(SqStack s) {
for(int i = 1; i <= s.top; i++)
cout << s.data[i] << " \n"[i == s.top];
}
int main() {
int n;
cin >> n;
SqStack s;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
push(s, x);
if(Is_full(s)) {
cout << "full!\n";
break;
}
}
int e;
pop(s, e), pop(s, e);
if(Is_empty(s))cout << "empty!\n";
print(s);
return 0;
}