class Stack:
def __init__(self):
self.items = []
def empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
def peek(self):
if not self.empty():
return self.items[len(self.items) - 1]
else:
return None
def pop(self):
if not self.empty():
return self.items.pop()
else:
print('栈已经为空')
def push(self, x):
self.items.append(x)
class MyStack:
def __init__(self):
self.A = Stack() # 用来存储栈中元素
self.B = Stack() # 用来存储当前栈中最小的元素
def push(self, data):
self.A.push(data)
def pop(self):
if self.B.empty():
while not self.A.empty():
self.B.push(self.A.peek())
self.A.pop()
first = self.B.peek()
self.B.pop()
return first
if __name__ == '__main__':
stack = MyStack()
stack.push(1)
stack.push(2)
stack.push(3)
print('队列首元素为:' + str(stack.pop()))
print('队列首元素为:' + str(stack.pop()))
stack.push(4)
print('队列首元素为:' + str(stack.pop()))
print('队列首元素为:' + str(stack.pop()))
运行结果如下:
队列首元素为:1
队列首元素为:2
队列首元素为:3
队列首元素为:4