Skip to content

Instantly share code, notes, and snippets.

@DaeHanJang
Created March 19, 2024 02:29
백준 10828 스택
#include <iostream>
#include <vector>
using namespace std;
class stack {
private:
int a[10000];
int i = -1;
public:
void push(int x);
int pop();
int size();
int empty();
int top();
};
void stack::push(int x) { a[++i] = x; }
int stack::pop() {
if (empty()) return -1;
else return a[i--];
}
int stack::size() { return (empty()) ? 0 : i + 1; }
int stack::empty() { return (i == -1) ? 1 : 0; }
int stack::top() {
if (empty()) return -1;
else return a[i];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
stack stk;
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (s == "push") {
int t;
cin >> t;
stk.push(t);
}
else if (s == "pop") cout << stk.pop() << '\n';
else if (s == "size") cout << stk.size() << '\n';
else if (s == "empty") cout << stk.empty() << '\n';
else if (s == "top") cout << stk.top() << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment