https://www.acmicpc.net/problem/18258
18258번: 큐 2
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
정적 배열 큐
fscanf로 입력받은 후fgetc(stdin)으로 버퍼를 비워주었다
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MALLOC(p,s) {\
if (!((p) = malloc(s))) {\
fprintf(stderr, "Insufficient memory.");\
exit(EXIT_FAILURE);\
}\
}
#define MAX_Q_SIZE 2000001
#define MAX(x,y) ((x) > (y)) ? (x):(y)
int queue[MAX_Q_SIZE];
int front = 0;
int rear = -1;
int main(void) {
int size;
int n, data;
char order[10];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", order);
fgetc(stdin); // 버퍼 비우기
size = rear - front + 1;
if (!strcmp(order, "push")) {
scanf("%d", &data);
fgetc(stdin); // 버퍼 비우기
queue[++rear] = data;
}
else if (!strcmp(order, "pop")) {
if (size == 0) printf("-1\n");
else {
printf("%d\n", queue[front++]);
}
}
else if (!strcmp(order, "size")) {
printf("%d\n", size);
}
else if (!strcmp(order, "empty")) {
if (size == 0) printf("1\n");
else printf("0\n");
}
else if (!strcmp(order, "front")) {
if (size == 0) printf("-1\n");
else printf("%d\n", queue[front]);
}
else if (!strcmp(order, "back")) {
if (size == 0) printf("-1\n");
else printf("%d\n", queue[rear]);
}
}
return 0;
}
'잡동사니 > [2022] 회로이론' 카테고리의 다른 글
[백준/C] 9012번 괄호 (스택) (0) | 2022.12.11 |
---|---|
[백준/C] 2559번 수열 (슬라이딩 윈도우, 누적합) (0) | 2022.12.11 |
[백준/C] 1152번 단어의 개수 (0) | 2022.12.11 |
[백준/C] 2563번 색종이 (0) | 2022.12.11 |
[백준/C] 11653번 소인수분해 (0) | 2022.12.11 |