class MyQueue:
def __init__(self):
self.stack_in = list()
self.stack_out = list()
def push(self, x: int) -> None:
self.stack_in.append(x)
def pop(self) -> int:
if self.empty():
return None
if self.stack_out:
return self.stack_out.pop()
else:
while self.stack_in:
self.stack_out.append(self.stack_in.pop())
return self.stack_out.pop()
def peek(self) -> int:
node = self.pop()
if node:
self.stack_out.append(node)
return node
def empty(self) -> bool:
return not (self.stack_in or self.stack_out)
class MyStack:
def __init__(self):
self.queue1 = list()
self.queue2 = list()
def push(self, x: int) -> None:
self.queue1.append(x)
def pop(self) -> int:
if self.empty():
return None
if self.queue1:
for i in range(len(self.queue1)-1):
self.queue2.append(self.queue1.pop(0))
res = self.queue1.pop(0)
self.queue1, self.queue2 = self.queue2, self.queue1
return res
def top(self) -> int:
res = self.pop()
if res:
self.push(res)
return res
def empty(self) -> bool:
return not self.queue1
标签:return,第十天,queue1,队列,self,pop,Python,stack,def
From: https://www.cnblogs.com/yixff/p/17779075.html