문제: https://www.acmicpc.net/problem/10828
t (=top)라는 변수 하나만 가지고 스택을 컨트롤하면 됩니다.
스택이 비어있는 조건은 top이 초기값일 때 입니다.
#define STACK_SIZE 10005
typedef struct STACK {
int S[STACK_SIZE];
int t, sz;
STACK() : t(-1), sz(0) {}
bool empty() {
return (t == -1);
}
int size() const {
return sz;
}
void push(int val) {
S[++t] = val;
sz++;
}
int top() {
if (empty()) return -1;
else return S[t];
}
int pop() {
if (empty()) return -1;
else {
int ret = top();
t--;
sz--;
return ret;
}
}
}STACK;
코드: https://github.com/cottory/algorithm/blob/master/BOJ/BOJ10828.cc