题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路
一个栈用来存储 pop时弹出stack2,stack2为空,pop出stack1存储在stack2中
class CQueue(object):
def __init__(self):
self.stack1=[]
self.stack2=[]
def appendTail(self,value):
self.stack1.append(value)
def deleteHead(self,value):
if not self.stack2:
while self.stack1:
self.stack2.append(self.stack1.pop(-1))
if len(self.stack2)==0:
return -1
return self.stack2.pop(-1)