입력 코드
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct deque{
int num;
struct deque* next;
struct deque* parent;
}deq;
deq* front=NULL;
deq* back=NULL;
int main(){
int N,X,size=0;
deq* a;
deq* del;
char sym[11];
scanf("%d",&N);
while(N--){
scanf("%s",sym);
if(!strcmp(sym,"push_front")){
scanf("%d",&X);
a=(deq*)malloc(sizeof(deq));
a->num=X;
a->parent=NULL;a->next=NULL;
if(front!=NULL){
front->parent=a;
a->next=front;
front=a;
}else{
front=a;
back=a;
}
size++;
}else if(!strcmp(sym,"push_back")){
scanf("%d",&X);
a=(deq*)malloc(sizeof(deq));
a->num=X;
a->parent=NULL;a->next=NULL;
if(back!=NULL){
back->next=a;
a->parent=back;
back=a;
}else{
front=a;
back=a;
}
size++;
}else if(!strcmp(sym,"pop_front")){
if(front!=NULL){
printf("%d\n",front->num);
del=front;
if(front->next!=NULL){
front=front->next;
front->parent=NULL;
free(del);
del=NULL;
}else{
free(front);
front=NULL;back=NULL;
}
size--;
}else printf("-1\n");
}else if(!strcmp(sym,"pop_back")){
if(back!=NULL){
printf("%d\n",back->num);
del=back;
if(back->parent!=NULL){
back=back->parent;
back->next=NULL;
free(del);
del=NULL;
}else{
free(back);
back=NULL;front=NULL;
}
size--;
}else printf("-1\n");
}else if(!strcmp(sym,"size")){
printf("%d\n",size);
}else if(!strcmp(sym,"empty")){
if(size==0)printf("1\n");
else printf("0\n");
}else if(!strcmp(sym,"front")){
if(size!=0)printf("%d\n",front->num);
else printf("-1\n");
}else if(!strcmp(sym,"back")){
if(size!=0)printf("%d\n",back->num);
else printf("-1\n");
}
}
return 0;
}
출처 https://blog.naver.com/kwleepuppy/221321633034
코드 설명
#자료구조 #덱
문제 출처
'C' 카테고리의 다른 글
#64. [백준_C언어] 11866 : 요세푸스 문제 0 (0) | 2021.02.03 |
---|---|
#63. [백준_C언어] 11650 : 좌표 정렬하기 (0) | 2021.02.03 |
#61. [백준_C언어] 10845 : 큐 (0) | 2021.02.02 |
#60. [백준_C언어] 10828 : 스택 (0) | 2021.02.01 |
#59. [백준_C언어] 10816 : 숫자 카드 2 (0) | 2021.02.01 |