백준 문제풀이(Baekjoon)
[Baekjoon] 10828번 : 스택
LIZ0904
2020. 6. 16. 01:46
반응형
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 �
www.acmicpc.net
정답 코드
#include <stdio.h>
#include <string.h>
#define MAX 10000
int stack[MAX];
int high = -1;
void push(int data) {
stack[++high] = data;
};
void pop() {
if (high ==-1) {
printf("-1\n");
return;
}
printf("%d\n", stack[high]);
stack[high--] = 0;
};
void size() {
printf("%d\n", high + 1);
};
void empty() {
if (high == -1) printf("1\n");
else printf("0\n");
};
void top() {
if (high == -1) {
printf("-1\n");
return;
}
printf("%d\n", stack[high]);
};
int main() {
int n,data;
char input[30];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", &input, 30);
if (strcmp(input,"push")==0) {
scanf("%d", &data);
push(data);
}
else if (strcmp(input, "pop") == 0) {
pop();
}
else if (strcmp(input, "size") == 0) {
size();
}
else if (strcmp(input, "empty") == 0) {
empty();
}
else if (strcmp(input, "top") == 0) {
top();
}
}
}
반응형