#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main() {
int n;
int m;
int p = 0; //스택의 현재 위치
int arr[100000] = { 0, };
char command[10];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", command);
if (strcmp(command, "push") == 0) {
scanf("%d", &m);
arr[p] = m;
p++;
}
else if (strcmp(command, "pop") == 0) {
if (p == 0)
printf("-1\n");
else {
printf("%d\n", arr[p - 1]);// 여기 이상
p--;
}
}
else if (strcmp(command, "size") == 0) {
printf("%d\n", p);
}
else if (strcmp(command, "empty") == 0) {
if (p == 0)
printf("1\n");
else
printf("0\n");
}
else {
if (p == 0) //arr[0] == 0 대신 p로 해야되는 이유
printf("-1\n");
else
printf("%d\n", arr[p - 1]);
}
}
}
어떤 문제 풀지 탐색하던 도중 스택이 눈에 띄었다. 아 오늘 자료구조만 하는 날인가봐,,
내일 다른 사람들의 자료 구조 풀이도 찾아 봐야겠다.
'프로그래밍 언어 > C언어' 카테고리의 다른 글
백준 > 2839 설탕 배달 (0) | 2021.12.24 |
---|---|
백준 > 1978 소수 찾기 (0) | 2021.12.24 |
백준 > 7568 덩치 (0) | 2021.12.21 |
백준 > 10866 덱 (0) | 2021.12.21 |
백준 > 10845 큐 (0) | 2021.12.21 |