입력 코드
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
int queue[2000000];
int front = 0, back = 0;
char command[6];
for (int i = 0; i<n; i++) {
scanf("%s", command);
if (strncmp(command, "push", 4) == 0) {
int num;
scanf("%d", &num);
queue[front] = num;
front += 1;
}
else if (strncmp(command, "pop", 3) == 0) {
if (front == back) printf("-1\n");
else {
printf("%d\n", queue[back]);
back += 1;
}
}
else if (strncmp(command, "size", 4) == 0) {
printf("%d\n", front - back);
}
else if (strncmp(command, "empty", 5) == 0) {
if (front == back) printf("1\n");
else printf("0\n");
}
else if (strncmp(command, "front", 5) == 0) {
if (front == back) printf("-1\n");
else printf("%d\n", queue[back]);
}
else if (strncmp(command, "back", 4) == 0) {
if (front == back) printf("-1\n");
else printf("%d\n", queue[front - 1]);
}
}
return 0;
}
출처 www.acmicpc.net/source/17560588
코드 설명
#자료구조 #큐
References
daily-life-in-20s.tistory.com/79
#61. [백준_C언어] 10845 : 큐
입력 코드 #include #include #include int q[10000]; int start = 0, end = 0, count = 0; void push(int x); void pop(); void size(); void empty(); void front(); void back(); void push(int x) { q[end+..
daily-life-in-20s.tistory.com
daily-life-in-20s.tistory.com/92
#01. [문자열 비교하기] strcmp 함수, strncmp 함수
strcmp 함수 문자열 비교(string compare) string.h 헤더 파일에 선언 strcmp(문자열1, 문자열2) strncmp 함수 길이를 지정해서 두 문자열을 비교하는 함수 string.h 헤더 파일에 선언 strncmp(문자열1, 문자열2,..
daily-life-in-20s.tistory.com
문제 출처
18258번: 큐 2
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
'C' 카테고리의 다른 글
#68. [백준_C언어] 1021 : 회전하는 큐 \ 자료구조 (0) | 2021.02.09 |
---|---|
#67. [백준_C언어] 1966 : 프린터 큐 \ 큐, 덱 (0) | 2021.02.08 |
#65. [백준_C언어] 10773 : 제로 \ 스택 (0) | 2021.02.08 |
#64. [백준_C언어] 11866 : 요세푸스 문제 0 (0) | 2021.02.03 |
#63. [백준_C언어] 11650 : 좌표 정렬하기 (0) | 2021.02.03 |