728x90
반응형
https://www.acmicpc.net/problem/18258
Python
import sys
input = sys.stdin.readline
class Node:
def __init__(self, data, link):
self.data = data
self.link = link
head = None
tail = None
length = 0
for _ in range(int(input())):
cmd = input().split()
if cmd[0][:2] == "pu":
if head == None:
head = tail = Node(cmd[1], None)
else:
tail.link = Node(cmd[1], None)
tail = tail.link
length += 1
elif cmd[0][:2] == "po":
if head == None:
print(-1)
else:
print(head.data)
head = head.link
if head == None:
tail = None
length -= 1
elif cmd[0][0] == "f":
print(-1 if head == None else head.data)
elif cmd[0][0] == "b":
print(-1 if tail == None else tail.data)
elif cmd[0][0] == "s":
print(length)
elif cmd[0][0] == "e":
print(1 if head == None else 0)
https://www.acmicpc.net/problem/10866
Python
import sys
input = sys.stdin.readline
class Node:
def __init__(self, data, link, before):
self.data = data
self.link = link
self.before = before
head = None
tail = None
length = 0
for _ in range(int(input())):
cmd = input().split()
if cmd[0][:4] == "push":
length += 1
if head == None:
head = tail = Node(cmd[1], None, None)
else:
if cmd[0][5:] == "back":
tail.link = Node(cmd[1], None, tail)
tail = tail.link
elif cmd[0][5:] == "front":
head.before = Node(cmd[1], head, None)
head = head.before
elif cmd[0][:3] == "pop":
if head == None:
print(-1)
else:
length -= 1
if cmd[0][4:] == "front":
print(head.data)
head = head.link
elif cmd[0][4:] == "back":
print(tail.data)
tail = tail.before
if length == 0:
head = tail = None
elif cmd[0][0] == "f":
print(-1 if head == None else head.data)
elif cmd[0][0] == "b":
print(-1 if tail == None else tail.data)
elif cmd[0][0] == "s":
print(length)
elif cmd[0][0] == "e":
print(1 if head == None else 0)
728x90
반응형
'Algorithm Problems' 카테고리의 다른 글
[백준][Python] 15828번 Router - 원형 큐 (0) | 2021.08.01 |
---|---|
[백준][Python] 2751번 수 정렬하기2 - MergeSort (0) | 2021.07.29 |
[백준/Python] 14717번 앉았다 - BruteForce (0) | 2021.07.20 |
[백준/Python] 숫자 정사각형 - BruteForce (0) | 2021.07.20 |
[백준] [Python] 2606번 바이러스 - BFS (0) | 2021.07.06 |