10866
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int front_idx;
int back_idx;
int size = 0;
// push_front X: 정수 X를 덱의 앞에 넣는다.
void push_front(int x, int *deque){
if(size == 0){
deque[front_idx] = x;
}
else{
deque[--front_idx] = x;
}
size++;
return;
}
// push_back X: 정수 X를 덱의 뒤에 넣는다.
void push_back(int x, int *deque){
if(size == 0){
deque[back_idx] = x;
}
else{
deque[++back_idx] = x;
}
size++;
return;
}
// pop_front: 덱의 가장 앞에 있는 수를 빼고, 그 수를 출력한다.
// 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
int pop_front(int *deque){
if(size == 0){
return -1;
}
int ret_v = deque[front_idx];
if(size != 1){
front_idx++;
}
size--;
return ret_v;
}
// pop_back: 덱의 가장 뒤에 있는 수를 빼고, 그 수를 출력한다.
// 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
int pop_back(int *deque){
if(size == 0){
return -1;
}
int ret_v = deque[back_idx];
if(size != 1){
back_idx--;
}
size--;
return ret_v;
}
// size: 덱에 들어있는 정수의 개수를 출력한다.
int f_size(){
return size;
}
// empty: 덱이 비어있으면 1을, 아니면 0을 출력한다.
int empty(){
if(size == 0){
return 1;
}
else {
return 0;
}
}
// front: 덱의 가장 앞에 있는 정수를 출력한다.
// 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
int front(int *deque){
if(size == 0){
return -1;
}
return deque[front_idx];
}
// back: 덱의 가장 뒤에 있는 정수를 출력한다.
// 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
int back(int *deque){
if(size == 0){
return -1;
}
return deque[back_idx];
}
int main(void){
const char *str_push_front = "push_front";
const char *str_push_back = "push_back";
const char *str_pop_front = "pop_front";
const char *str_pop_back = "pop_back";
const char *str_size = "size";
const char *str_empty = "empty";
const char *str_front = "front";
const char *str_back = "back";
// 테스트 케이스 입력
int test_case = 0;
scanf("%d", &test_case);
front_idx = test_case;
back_idx = test_case;
int *deque = (int *)malloc(sizeof(int)*test_case*2);
for(int i = 0; i < test_case; i++){
// 명령어 입력
char command[11] = {0};
getchar();
scanf("%s", command);
if(strcmp(str_push_front, command)==0){
int num = 0;
scanf("%d", &num);
push_front(num, deque);
}
else if(strcmp(str_push_back, command)==0){
int num = 0;
scanf("%d", &num);
push_back(num, deque);
}
else if(strcmp(str_pop_front, command)==0){
printf("%d\n",pop_front(deque));
}
else if(strcmp(str_pop_back, command)==0){
printf("%d\n",pop_back(deque));
}
else if(strcmp(str_size, command)==0){
printf("%d\n",f_size());
}
else if(strcmp(str_empty, command)==0){
printf("%d\n",empty());
}
else if(strcmp(str_front, command)==0){
printf("%d\n",front(deque));
}
else if(strcmp(str_back, command)==0){
printf("%d\n",back(deque));
}
}
free(deque);
return 0;
}


제대로 된 deque는 아니지만 조금 설명을 해보자면,,
5(명령어 개수) * 2의 배열 만들어주고
front, back 인덱스 시작을 5(명령어 개수) 로 인덱스로 잡는다.
만약 처음 push (즉 size가 0일 때 push)
5(명령어 개수)자리에 먼저 값을 채워넣고 ++혹은 --는 안해줌.
그 이후에 push면 ++ 혹은 -- 한 후 그 자리에 값을 채워넣음
비슷하게
size가 1일 때 pop은 내보낼 값을 저장 후 -- 혹은 ++ 는 안해주고, size만 줄여(size == 0) 다시 처음 push가 가능한 상태로 만들고 값을 내보냄.
size가 1 이상일 때 pop은 내보낼 값을 저장 후 --혹은 ++ 을 하고, size도 줄여서 값을 내보냄.
만약 명령어 개수가 무한하다면 절대 안되는 코드에유..... ㅎ........
'백준' 카테고리의 다른 글
| [ 백준 / C ] 16189번 : Repetitive Palindrome (0) | 2025.12.08 |
|---|---|
| [ 백준 / C ] 15720번 : 카우버거 (0) | 2025.12.05 |
| [ 백준 / C ] 1158번 : 요세푸스 문제 (0) | 2025.12.03 |
| [ 백준 / C ] 1406번 : 에디터 (0) | 2025.12.02 |
| [ 백준 / C ] 25757번 : 임스와 함께하는 미니게임 (0) | 2025.12.01 |