학과공부/자료구조

[백준] 10845번 - 큐 (By Python)

tlsrhksdn1 2022. 7. 24. 19:56

문제설명

 

풀이

 

import sys

N=int(sys.stdin.readline())
queue=[]

for i in range(N):
    words=sys.stdin.readline().split()

    
    if words[0]=='push':
        queue.append(words[1])
        
    elif words[0]=='pop':
        if not queue:
            print(-1)
        else:
            print(queue.pop(0))
            
    elif words[0]=='size':
        print(len(queue))
        
    elif words[0]=='empty':
        if queue:
            print(0)
        else:
            print(1)
            
    elif words[0]=='front':
        if queue:
            print(queue[0])
        else:
            print(-1)
            
    elif words[0]=='back':
        if queue:
            print(queue[-1])
        else:
            print(-1)