# coding=utf-8

MAX_LENGTH = 100

SUCCESS = 1
FAIL = 0
ERROR = -1 class Queue(object): stack_fir = None
stack_sec = None
queue_len = 0 def __init__(self):
self.stack_fir = Stack()
self.stack_sec = Stack()
self.queue_len = 0 # 队列push方法
def push(self, item):
if self.queue_len == MAX_LENGTH:
return Result(FAIL, None)
elif self.queue_len > MAX_LENGTH:
return Result(ERROR, None) self.queue_len += 1
result = self.stack_fir.push(item)
if result.get_code() == SUCCESS:
return Result(SUCCESS, None)
else:
return Result(FAIL, None) # 队列pop方法
def pop(self):
if self.queue_len == 0:
return Result(FAIL, None)
elif self.queue_len < 0:
return Result(ERROR, None) self.queue_len -= 1 # 将装有数据的栈拷贝到空栈中
while self.stack_fir.has_next():
pop_result = self.stack_fir.pop()
if pop_result.get_code() == SUCCESS:
self.stack_sec.push(pop_result.get_value()) result = self.stack_sec.pop().get_value() # 将装有数据的栈拷贝到空栈中
while self.stack_sec.has_next():
pop_result = self.stack_sec.pop()
if pop_result.get_code() == SUCCESS:
self.stack_fir.push(pop_result.get_value()) return Result(SUCCESS, result) # 队列是否有下一个元素
def has_next(self):
return self.queue_len > 0 class Stack(object): stack_len = 0
item_list = [] def __init__(self):
self.stack_len = 0
self.item_list = [] # 栈push方法
def push(self, item):
if self.stack_len == MAX_LENGTH:
return Result(FAIL, None)
elif self.stack_len > MAX_LENGTH:
return Result(ERROR, None) self.stack_len += 1
self.item_list.append(item)
return Result(SUCCESS, None) # 栈pop方法
def pop(self):
if self.stack_len == 0:
return Result(FAIL, None)
elif self.stack_len < 0:
return Result(ERROR, None) self.stack_len -= 1
item = self.item_list[self.stack_len]
del self.item_list[self.stack_len]
return Result(SUCCESS, item) # 栈是否有下一个元素
def has_next(self):
return self.stack_len > 0 class Result(object):
code = 0
value = None def __init__(self, code, value):
self.code = code
self.value = value def get_code(self):
return self.code def get_value(self):
return self.value if __name__ == '__main__':
queue = Queue()
queue.push(1)
queue.push(2)
queue.push(5)
queue.push(4)
queue.push(3) while queue.has_next():
print queue.pop().get_value()

  

两个栈实现队列 Python实现的更多相关文章

  1. 用两个栈实现队列(python)

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. # -*- coding:utf-8 -*- class Solution: def __init__( ...

  2. 两个栈实现队列 牛客网 程序员面试金典 C++ Python

    两个栈实现队列 牛客网 程序员面试金典 C++ Python 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. C++ //run:5ms memeory ...

  3. 用两个栈实现队列与用两个队列实现栈(Python实现)

    用两个栈实现队列: class QueueWithTwoStacks(object): def __init__(self): self._stack1 = [] self._stack2 = [] ...

  4. 《剑指offer》用两个栈实现队列

    本题来自<剑指offer> 用两个栈实现队列 题目: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路: 队列定义:先进先出 栈定义:先进后出 要 ...

  5. 剑指Offer面试题:6.用两个栈实现队列

    一.题目:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...

  6. 剑指OFFER之用两个栈实现队列(九度OJ1512)

    题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作.队列中的元素为int类型. 输入: 每个输入文件包含一个测试样例.对于每个测试样例,第一行输入一个n(1<=n<=100 ...

  7. 九度OJ 1512 用两个栈实现队列 【数据结构】

    题目地址:http://ac.jobdu.com/problem.php?pid=1512 题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 输入: 每 ...

  8. 两个栈实现队列+两个队列实现栈----java

                                               两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...

  9. Algorithm --> 两个栈实现队列和两个队列实现栈

    两个栈实现队列和两个队列实现栈 队列(queue)先进先出的线性表:栈(stack)先进后出的线性表. 两个栈实现队列 法一思路: s1是入栈的,s2是出栈的. 入队列:直接压入s1即可: 出队列:如 ...

随机推荐

  1. Binary file to C array(bin2c)

    /******************************************************************************** * Binary file to C ...

  2. 【opencv基础】测量运行时间的函数getTickCount/getCPUTickCount/getTickFrequency

    函数的计算结果类型是double,单位是秒. 要使用更精确的计时,就需要使用getCPUTickCount(),不过现代计算机CPU的频率会随着负载而变化所以没大有必要使用该函数,可以参看函数的介绍[ ...

  3. Android中对文件的读写进行操作

    1. 在文件的地方生成一个read.txt文件,并且写入一个read数据.IO流用完之后一定要记得关闭. 对于try和catch是对于错误的抓取. 2. 首先先new file来找到那个文件,然后在通 ...

  4. Hash表的平均查找长度ASL计算方法

    Hash表的“查找成功的ASL”和“查找不成功的ASL” ASL指的是 平均查找时间 关键字序列:(7.8.30.11.18.9.14) 散列函数: H(Key) = (key x 3) MOD 7 ...

  5. java时间处理--持续时间格式化工具和常量类DurationFormatUtils

    阅读目录 DurationFormatUtils类简介 maven地址 构造方法 DurationFormatUtils() formatDurationHMS(long durationMillis ...

  6. CH4201 楼兰图腾

    题意 4201 楼兰图腾 0x40「数据结构进阶」例题 描述 在完成了分配任务之后,西部314来到了楼兰古城的西部.相传很久以前这片土地上(比楼兰古城还早)生活着两个部落,一个部落崇拜尖刀('V'), ...

  7. benthos 几个方便的帮助命令

    benthos 的命令行帮助做的是比较方便的,基本上就是一个自包含的帮助文档 全部命令 benthos --help 查询系统支持的caches benthos -list-caches 说明 使用帮 ...

  8. Oracle C#处理时间类型的Insert

    首先如果直接   parm.Value=DateTime.Now;   insert into table (TheTime)Value(@parm);   执行sql就会报错 ----------- ...

  9. SOALog

    项目地址 :  https://github.com/kelin-xycs/SOALog SOALog 为 SOA 架构 提供一种 松耦合 乐观 的 数据一致性 解决方案,说白了这个组件的功能就是 记 ...

  10. RAC3——RAC原理开始

    1.RAC并发 RAC的本质是一个数据库,只不过现在这个数据库运行在了多台计算机上,在原先的单实例中,一个进程是否可以修改一条数据,取决于是否有其他进程(同一台计算机上)并发修改.在RAC环境下,这种 ...