#62. [백준_C언어] 10866 : 덱

 

 입력 코드 

#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

 

 

 

 코드 설명 

#자료구조 #덱

 

 

 

 문제 출처 

www.acmicpc.net/problem/10866

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net