본문 바로가기
Algorithm

[알고리즘] 덱 백준 10866 (Python)

by 가영리 2022. 1. 16.
728x90

큐와 비슷하지만 덱은 양쪽에서 push, pop이 가능하다.

 

코드

import sys

n  = int(input())
deque = []

for i in range(n):
    command = sys.stdin.readline().split()

    if command[0] == 'push_front' : 
      deque.insert(0,command[1])

    elif command[0] == 'push_back' :
       deque.append(command[1])
    elif command[0] == "pop_front" : 
        if len(deque) == 0 : 
          print(-1)
        else : 
          print(deque.pop(0))
    elif command[0] == "pop_back" : 
        if len(deque) == 0 : print(-1)
        else : print(deque.pop(len(deque)-1))
    
    elif command[0] == 'size':
       print(len(deque))
    elif command[0] == 'empty':
        if len(deque) == 0 :
           print(1)
        else :
           print(0)
    elif command[0] == 'front':
        if len(deque) == 0 :
           print(-1)
        else : 
          print(deque[0])
    elif command[0] == 'back' :
        if len(deque) == 0 :
           print(-1)
        else :
          print(deque[-1])

 

입출력