栈的数组模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
const int N = 100010;
int stack[N], top, m;
string mode;

void init() {
top = 0;
}

void push(int x) {
stack[++top] = x;
}

void pop() {
top--;
}

int size() {
return top;
}

int main() {
cin >> m;
init();
while(m--) {
int x;
cin >> mode;
if(mode == "push") cin >> x, push(x);
if(mode == "pop") pop();
if(mode == "empty") cout << (size() == 0 ? "YES\n" : "NO\n");
if(mode == "query") cout << stack[top] << endl;
}
}
Author

InverseDa

Posted on

2021-03-17

Updated on

2023-03-30

Licensed under

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×