Software/C
[Baekjoon C] 10828 스택
보갬미
2021. 2. 19. 00:20
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
문제
코드
#include <stdio.h>
#include <string.h>
int stack[100001];
int count = 0;
void push(int x);
void pop();
void size();
void empty();
void top();
int main() {
int n, x;
char order[6];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", &order);
if (!strcmp(order, "push")) {
scanf("%d", &x);
push(x);
}
else if (!strcmp(order, "pop")) pop();
else if (!strcmp(order, "size")) size();
else if (!strcmp(order, "empty")) empty();
else if (!strcmp(order, "top")) top();
else break;
}
return 0;
}
void push(int x) {
stack[count] = x;
count++;
}
void pop() {
if (count != 0) {
count--;
printf("%d\n", stack[count]);
stack[count] = 0;
}
else printf("%d\n", -1);
}
void size() {
printf("%d\n", count);
}
void empty() {
if (count == 0) printf("%d\n", 1);
else printf("%d\n", 0);
}
void top() {
if (count > 0)
printf("%d\n", stack[count-1]);
else printf("%d\n", -1);
}