class MyStack(object):
def __init__(self):
self.lst = list()
def push(self, x):
self.lst.append(x)
def pop(self):
return self.lst.pop()
def size(self):
return len(self.lst)
class MyQueue(object):
def __init__(self, max_size):
self.stack1 = MyStack()
self.stack2 = MyStack()
self.max_size = max_size
def qsize(self):
return self.stack1.size()
def push(self, item):
if self.qsize()>=self.max_size:
return False
else:
self.stack1.push(item)
return True
def pop(self):
try:
if self.stack1.size() == 0:
raise emptyException
while self.stack1.size() > 0:
self.stack2.push(self.stack1.pop())
item = self.stack2.pop()
while self.stack2.size() > 0:
self.stack1.push(self.stack2.pop())
return item
except emptyException:
return False
class emptyException(Exception):
pass
n, max_size = map(int, input().strip().split(' '))
my_queue=MyQueue(max_size)
for _ in range(n):
command=input().split()
if command[0]=="PUSH":
print(my_queue.push(int(command[1])))
elif command[0]=="POP":
print(my_queue.pop())
elif command[0]=="SIZE":
print(my_queue.qsize())
'Computer Science > 자료구조와 알고리즘' 카테고리의 다른 글
[python] queue사용법( Queue module의 Queue class) (0) | 2021.01.11 |
---|---|
[python] queue 사용법 (collection 모듈 deque) (0) | 2021.01.11 |
1. 선형배열 (0) | 2020.12.25 |