#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/5/24 下午4:47
# @Author : Yuxiaoxue# @Site :
# @File : ques5.py
# @Software: PyCharm
'''
题目描述:
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
'''
stack1 = []
stack2 = []
def push(node):
stack1.append(node)
def pop():
if stack2 == []:
while stack1:
top = stack1.pop()
stack2.append(top)
if stack2 != []:
print(stack2[-1])
top = stack2[-1]
stack2.pop()
return top
push(1)
push(2)
push(3)
top = pop()
top = pop()
push(4)
top = pop()
push(5)
top = pop()
top = pop()
'''
牛客网最终提交答案;
class Solution:
def __init__(self):
self.stack1 = []
self.stack2 = []
def push(self, node):
# write code here
self.stack1.append(node)
def pop(self):
# return xx
if self.stack2 == []:
while self.stack1:
top = self.stack1.pop()
self.stack2.append(top)
if self.stack2 != []:
top = self.stack2[-1]
self.stack2.pop()
return top
'''
网友评论