프로그래밍 언어/C언어

백준 > 10845 큐

B612 2021. 12. 21. 11:44
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main() {
	int n, i;
	int arr[10000] = { 0, }; // 큐
	char command[10];
	int m;
	int p = 0; // 현재 스택 위치

	scanf("%d", &n);

	for (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 (arr[0] == 0) {
				printf("-1\n");
			}
			else {
				printf("%d\n", arr[0]);
				for (int t = 0; t < p; t++) {
					arr[t] = arr[t + 1];
				}
				p--;
			}
		}
		else if (strcmp(command, "empty") == 0) {
			if (arr[0] == 0)
				printf("1\n");
			else
				printf("0\n");
		}
		else if (strcmp(command, "front") == 0) {
			if (arr[0] == 0)
				printf("-1\n");
			else
				printf("%d\n", arr[0]);
		}
		else if (strcmp(command, "size") == 0) {
			printf("%d\n", p);
		}
		else
			if (arr[0] == 0)
				printf("-1\n");
			else
				printf("%d\n", arr[p - 1]);
	}
}

놓친 부분: 배열 초기화, scanf에 \n 넣어서 하나씩 밀린 거

스택에 영향을 주는 건 push와 pop 뿐이라는 것을 생각하면 어렵지 않게 풀 수 있을 것이다.

'프로그래밍 언어 > C언어' 카테고리의 다른 글

백준 > 7568 덩치  (0) 2021.12.21
백준 > 10866 덱  (0) 2021.12.21
백준 > 4153 직각삼각형  (0) 2021.12.20
백준 > 2609 최대공약수와 최소공배수  (0) 2021.12.19
CODE GROUND > 연습문제 > 근로 시간  (0) 2021.07.29