用栈定义队列(出入栈)
class Queue:
def __init__(self):
self.pushstack = []
self.pullstack = []
def push(self, ob):
self.pushstack.append(ob)
def pull(self):
if self.pullstack:
return self.pullstack.pop(-1)
else:
if not self.pushstack:
return '&&'
while self.pushstack:
self.pullstack.append(self.pushstack.pop(-1))
return self.pullstack.pop(-1)
用队列定义栈(数据队列和辅助队列)
class Stack:
def __init__(self):
self.Data = []
self.Help = []
def push(self, ob):
self.Data.append(ob)
def swap(self):
self.Data, self.Help = self.Help, self.Data
def pop(self):
while len(self.Data) != 1:
self.Help.append(self.Data.pop(0))
self.swap()
return self.Help.pop(0)