[문제]
https://www.acmicpc.net/problem/10866
[풀이]
2021.07.26 - [BOJ] - [Python] BOJ/백준 10845번 큐
와 같다.
[코드]
import sys
class deque():
def __init__(self):
self.queue = []
self.size = 0
def push_front(self, x):
self.queue.insert(0, x)
self.size += 1
def push_back(self, x):
self.queue.append(x)
self.size += 1
def pop_front(self):
if self.size == 0:
return -1
else:
self.size -= 1
return self.queue.pop(0)
def pop_back(self):
if self.size == 0:
return -1
else:
self.size -= 1
return self.queue.pop(-1)
def my_size(self):
return self.size
def empty(self):
if self.size == 0:
return 1
else:
return 0
def front(self):
if self.size == 0:
return -1
else:
return self.queue[0]
def back(self):
if self.size == 0:
return -1
else:
return self.queue[-1]
if __name__ == '__main__':
queue = deque()
n = int(sys.stdin.readline())
for _ in range(n):
command = sys.stdin.readline().split()
if command[0] == 'push_front':
queue.push_front(command[1])
elif command[0] == 'push_back':
queue.push_back(command[1])
elif command[0] == 'pop_front':
sys.stdout.write(str(queue.pop_front()))
sys.stdout.write("\n")
elif command[0] == 'pop_back':
sys.stdout.write(str(queue.pop_back()))
sys.stdout.write("\n")
elif command[0] == 'empty':
sys.stdout.write(str(queue.empty()))
sys.stdout.write("\n")
elif command[0] == 'size':
sys.stdout.write(str(queue.my_size()))
sys.stdout.write("\n")
elif command[0] == 'front':
sys.stdout.write(str(queue.front()))
sys.stdout.write("\n")
elif command[0] == 'back':
sys.stdout.write(str(queue.back()))
sys.stdout.write("\n")
'문제풀이 > BOJ' 카테고리의 다른 글
[Python] BOJ/백준 1018번 체스판 다시 칠하기 (0) | 2021.07.26 |
---|---|
[Python] BOJ/백준 10828번 스택 (0) | 2021.07.26 |
[Python] BOJ/백준 10845번 큐 (0) | 2021.07.26 |
[Python] BOJ/백준 2751번 수 정렬하기 2 (0) | 2021.07.24 |
[Python] BOJ/백준 2164번 카드2 (0) | 2021.07.24 |